I'm developping a shiny app displaying a leaflet map. I'm facing the pretty same situation than this topic : https://stackoverflow.com/questions/53070730/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)),
tabPanel("Table", dataTableOutput("tableannee"))
)
),
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)
})
output$tableannee <- renderDataTable({
datatable( data = selectedData(),
options = list(pageLength = 10),
rownames = FALSE)
})
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 : https://stackoverflow.com/questions/10865095/why-do-i-get-warning-longer-object-length-is-not-a-multiple-of-shorter-object-l
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?