Reputation: 76
I recently added a highchart to my shiny dashboard that reacts to clicking in specific polygons inside a leaflet map.
Depending on the polygon clicked, the data for the highchart is either taken from 1 dataframe, or a combination of 2 dataframes.
This works. However, I also have a 'map reset' button which resets the polygons clicked on the map. I am trying to code it so this reset button also resets the highchart.
However, all options I tried so far result in a blank highchart from the start, which doesn't change.
df1<- data.frame(location = c(3,4), "2013" = c(900, 100), "2014" = c(700, 600))
df2<- data.frame(location = c(1,2), "2013" = c(1400, 1500),
"2014" = c(1600, 1700), , location1 = c(3,4)))
click_shape <- eventReactive(input$map_shape_click, {
c <- input$map_shape_click$id
return(c)
})
data_for_chart <- reactive({
# if top level polygon, return data from that level
if(is.null(click_shape())) {return(df1[FALSE,])
}else if(click_shape() %in% df1){
c<- tolower(click_shape())
return(df1[df$location == c,] %>% gather(key = "year", value = GHA_N, 2:3))
# if second level, return data from top + second level
}else if(click_shape() %in% df2){
c<- tolower(click_shape())
d<- tolower(df2$location[df2$location == click_shape()])
return(df1[df1$location == d,] %>%
rbind(df2[df2$location == c,]) %>% gather(key = "year", value = GHA_N, 2:3))
# if no appropriate data is found, don't plot
}else{
return(df1[FALSE,])
}
})
I tried adding the reset button to the eventreactive like so:
click_shape <- eventReactive(c(input$map_shape_click,input$reset) {
if(input$map_shape_click) {
c <- input$map_shape_click$id
return(c)
}else{
return(NULL)
})
thinking the data_for_chart function would react to this by not plotting the data. However, it keeps the highchart blank from the moment you start the shiny app!
Upvotes: 0
Views: 156
Reputation: 76
I solved my own question:
by first adding a reactive value which contains the click_shape event
value<- reactiveValues(click_shape = NULL)
and then adding a second observe event that modifies the click_shape
observeEvent(input$reset, {
value$click_shape <- NULL
})
next to the original observeEvent
observeEvent(input$map_shape_click, {
value$click_shape <- input$map_shape_click$id
})
this way, two observe events can modify the click_shape event.
Upvotes: 0