User new to Shiny here. I am trying to create an interactive map which adds circle markers based on 2 groups of users inputs: where each group has 3 possible inputs. Within each group there is quite a bit of conditional execution that needs to be done to filter the data differently based on user input. However, I can't seem to figure out how to get both groups onto the map. Instead only one group shows if I simply repeat the code for the second group of input. How do I:
1) Get both groups to show on the map
2) Avoid having to repeat the conditional execution code if possible
3) Colour and size them based on the group on inputs (e.g. have circle markers map to denomination size if the circles are denominations, but have it mapped to religion population size if circles are religious groups instead.
My code below:
server <- function(input, output) {
output$mymap <- renderLeaflet({
input$map
x <- all_cleaned %>% filter(year == input$years_map)
isolate({
x <- x %>% filter(country_name == input$geography_1_map)
if(input$religion_1_map == "All" & input$denom_1_map == "All") {
x %>% create_map()
}
else if(input$religion_1_map == "All" & input$denom_1_map == "None") {
x[!duplicated(x[,'religion']), ] %>% create_map()
}
else if(input$denom_1_map == "None") {
x <- x %>% filter(religion == input$religion_1_map)
x[!duplicated(x[,'religion']), ] %>% create_map()
}
else if(input$denom_1_map == "All") {
x %>% filter(religion == input$religion_1_map) %>% create_map()
}
else {
x %>% filter(religion == input$religion_1_map, denom == input$denom_1_map) %>%
create_map()
}
})
})
create_map <- function(data) {
data %>% leaflet %>% addTiles %>%
addCircleMarkers(popup = ~paste("<b>", country_name, "</b>", "<br>",
religion, "<br>", religion_population, "<br>", religion_pct,
"<br>", denom, "<br>", denom_pop), radius =
~log(religion_population))
}
}
Snapshot of what I currently have:
A snapshot of my data, which is currently in Tidy format:
Thanks in advance - really appreciate any help or comments!!
Aucun commentaire:
Enregistrer un commentaire