I want to implement two map click events in my map, map_marker_click, and map_click. When I click on a marker on the map(using map_marker_click), I want the event to filter other related charts based on the id of the clicked marker. Using the map_click event by clicking the base map, I want the event to reset the map and graphs to default (with all the markers and charts). With my current code, I can only be able to click on a marker and only that marker is displayed on the map with the corresponding chart. Here is my code:
#reactive event for map click
map_click<-reactive({
clk=input$maps2_marker_click
if(is.null(clk$id)){
return(final_data)
}
else{
rev_filter<-final_data %>% filter(Kiosk_Id_number==clk$id)
return(rev_filter)
}
}
)
#color function for waterpoint types
pal=colorFactor(c("sienna2","green3","cornflowerblue"),domain = final_data$Water_point_type)
#interactive map with click event
output$maps2<-renderLeaflet(
leaflet() %>%
addCircleMarkers(
data = map_click(),
fillColor = ~pal(Water_point_type),
fillOpacity = 1.0,
radius = 8,
stroke = FALSE,
lat = ~latitude,
lng = ~longitude,
layerId = ~Kiosk_Id_number
) #end renderleaflet
#facet plots
#plots are filtered based on the map click
output$facet_plot<-renderPlotly(
ggplotly(
ggplot(map_click(),aes(x=Day,y=Daily_revenue,group=Kiosk_Id_number))+
geom_col(aes(fill=Water_point_type))+
scale_fill_manual(values = c("sienna2","cornflowerblue")
)+
geom_line(aes(y=Daily_Average),linetype="dashed", size=0.4,color="gray0")+
facet_wrap(~Kiosk_Id_number,scales = "free_y",ncol = 2)+
labs(
y="Daily Average Revenue (GHC)")+
scale_x_discrete(breaks=c("05-Jun-19","31-Jul-19",
"27-Sep-19","28-Nov-19","31-Dec-19",
"31-Jan-20","28-Feb-20","26-Mar-20"))+
scale_y_continuous(breaks = my_breaks)+
theme(axis.text.x = element_text(angle = 90,size = 7),
axis.title = element_text(size=9),
axis.text = element_text(face = "bold"),
axis.title.x = element_blank(),
axis.text.y = element_text(size=6,face="bold"),
strip.text = element_text(face="bold"),
strip.background = element_blank(),
strip.text.x = element_text(face="bold.italic",colour ="gray0",size=8,margin=margin(0,0,0,0,"cm")),
legend.title = element_blank(),
#legend.position = "bottom",
panel.grid = element_blank(),
panel.spacing = unit(2, "lines")
)
)
)#end renderplot