I'm developping a shiny app diplaying a leaflet map. I'm facing the pretty same situation than this topic : Change Leaflet Map Dynamically based on Multiple Reactive expressions
I tried to subset the dataframe according to multiple input selection in order to create a dynamic map. But I didn't succeed to deal with the vector lenght error "longer object length is not a multiple of shorter object length". This was fixed in the other topic with changing the "==" operator by "%in%", but not in my case.
My code:
all_year <- sort(unique(sample_testsf$annee))
all_area <- sort(unique(sample_testsf$nomzone))
ui <- fluidPage(
titlePanel("AgriPAG"),
sidebarLayout(
position = "right",
mainPanel(
tabsetPanel(
tabPanel("Map", leafletOutput('mymap',width = "100%", height = 1000))
)
),
sidebarPanel(
selectInput(
inputId = "year",
label = "Select a year to display",
choices = all_year,
selected = NULL,
multiple = TRUE,
selectize = FALSE
),
selectInput(
inputId = "area",
label = "Select a district",
choices = all_area,
selected = NULL,
multiple = TRUE,
selectize = FALSE
)
)
)
)
server <- function(input,output){
output$mymap <- renderLeaflet({
leaflet(data = sample_testsf) %>%
addTiles() %>%
setView(lng=-52.3333300, lat=4.9333300 , zoom=5)
})
selectedData <- reactive({
req(input$year)
req(input$area)
sample_testsf %>%
dplyr::filter(all_year %in% input$year & all_area %in% input$area
)
})
observe({
leafletProxy("mymap", data = selectedData()) %>%
clearShapes() %>%
addPolygons(weight=2, col="black", opacity=0.5)
})
}
shinyApp(ui = ui, server = server)
I actually understand why this problem is occuring (Why do I get "warning longer object length is not a multiple of shorter object length"?) and I guess I should use the recycling function but I do not know how to set it up. Anyone have an idea how to solve this problem?
I found my mistake, I was calling for the input choice list instead of the selected input in the reactive function. The solution :
selectedData <- reactive({
req(input$year)
req(input$area)
sample_testsf %>%
dplyr::filter(annee %in% input$year & nomzone%in% input$area
)
})