I am having trouble with using a switch input to switch between maps in Shiny dashboard. I am using radioGroupButtons
as my switch input. However this does not seem to work properly as it only shows the first map once (see gif below).
myMap1 output is only showing once
I have attempted to use other switch inputs like checkboxInput
and checkboxGroupButtons
but the result remains the same.
Reproducible code
library(recharts); library(echarts4r); library(echarts4r.maps)
library(shiny); library(shinyWidgets); library(shinydashboard)
library(purrr)
data_by_region <- data.frame(
Name = rep("A", 8),
Region = rep("Northland", 8),
long = rep(174.3223, 8),
lat = rep(-35.7047, 8),
Year = 2013:2020,
Amount = c(227, 252, 373, 363, 287, 307, 308, 293)
)
# Remove Chatham Island for echarts4r maps
nz_file <- system.file("New_Zealand.json", package = "echarts4r.maps")
nz_json <- jsonlite::read_json(nz_file)
# get names of polygons
names <- nz_json$features %>%
map("properties") %>%
map("name") %>%
unlist()
island_index <- grep("Chatham", names)
nz_json$features[[island_index]] <- NULL
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
selectInput(inputId = "year",
label = "pick a year",
choices = unique(factor(data_by_region$Year))),
radioGroupButtons(
inputId = "switchMap", label = NULL, justified = TRUE,
choiceNames = c("Map", "Advanced"),
choiceValues = c("Map1", "Map2"),
status = "primary"
),
fluidRow(column(6, uiOutput(outputId = "map")))
)
)
server = function(input, output, session) {
output$map <- renderUI({
if(input$switchMap == "Map1"){
echarts4rOutput(outputId = "myMap1")
}else{
eChartOutput(outputId = "myMap2")
}
})
output$myMap1 <- renderEcharts4r({
data_by_year <- data_by_region[data_by_region$Year == req(input$year),]
data_by_year <- data.frame(data_by_year)
data_by_year$Region <- factor(data_by_year$Region)
data_by_year %>%
e_charts(Region) %>%
e_map_register("NZ", nz_json) %>%
e_map(Amount, map = "NZ") %>%
e_visual_map(
Amount,
top = "20%",
left = "0%",
inRange = list(color = c("#3366FF","#6699FF", "#66CCFF", "#33CCFF")),
type = "piecewise",
splitList = list(
list(min = 300),
list(min = 250, max = 300),
list(min = 100, max = 250),
list(value = 0, label = "None")
))
})
output$myMap2 <- renderEChart({
yeardata <- data_by_region[data_by_region$Year == input$year, ]
top5dat <- as.data.frame(yeardata) %>% top_n(5)
top5dat <- data.frame(top5dat)
names(top5dat) <- c('Family', 'name', 'lng', 'lat', 'Year', 'value')
echartr(NULL, type='map_world', subtype = 'New Zealand') %>%
addMP(series = 'Top 5',
data = top5dat,
symbol = 'pin',
symbolSize = JS('function (v) {return 10 + v/50;}'),
effect = list(show = TRUE),
itemStyle = list(normal = itemStyle(color = "#EE82EE"))
) %>%
addGeoCoord(top5dat[, c('name', 'lng', 'lat')]) %>%
setToolbox(show = FALSE) %>%
setSeries(hoverable = FALSE, itemStyle=list(
normal = itemStyle(
labelStyle = labelStyle(color="#EE82EE"),
borderColor = 'rgba(100,149,237,1)', borderWidth = 0.5,
areaStyle = areaStyle(color='#1b1b1b')))) %>%
setLegend(show = FALSE) %>%
setTitle('Advanced map', 'Fictious Data', pos = 11)
})
}
shinyApp(ui, server)
Any help or hint would be greatly appreciated. Thanks