reactive filter using multiple inputs in Shiny

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?

1 Like

Hi @rlucas just FYI I am not able to run the code. If your code is reproducible your are likely to get better help.

Error in unique(sample_testsf$annee) : object 'sample_testsf' not found
1 Like

If I understand your code correctly you want to filter our sample_testsf dataframe depending on the choises made in input$year and input$area? If so, try

dplyr::filter(annee %in% input$year & nomzone %in% input$area)

Cheers
Steen

2 Likes

Thank you so much @stkrog, It was quite obvious but you know when you're stuck in your code it's like beeing blind...

I'm sorry for the non reproductible example but I get some trouble providing a spatial dataframe in reprex... I made some search but I did'nt found anything on the topic.

@rlucas glad you got it solved!

maybe the next time you can make use of a dataset that are included with packages you are using. So everyone using your package of choice has the data as well
data(package = .packages(all.available = TRUE))

2 Likes

Thanks for the tip! I'll do it for sure.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.