0

In the example Data Frame DF, I have the following columns.

gender <- c("Male", "Female","Female", "Male")
Location <- c("AB", "BC", "CD", "DE")
hasTV <- c("Yes","Yes","No","No")
Latitude <- c(49.82380908513249,59.478568831926395,59.478568831926395,49.82380908513249)
Longitude <- c(-10.8544921875,-10.8544921875,2.021484375,2.021484375)
DF <- data.frame(gender,Location,hasTV,Latitude,Longitude)

Under UI, i've used radiobuttons to select options from hasTV, checkboxGroupInput to select Gender and selectInput to create a dropdown of Location. I've implemented this in fluidRow as shown below.

sex <- unique(DF$gender)
loc <- unique(DF$Location)
hastv <- unique(DF$hasTV)

radioButtons("radio_hastv",label = "Has TV", choices = hastv, selected = "Yes")
checkboxGroupInput("checkbox_gender", label = "Gender", choices = sex, selected = sex)
selectInput("Location", label = "Location", choices=loc, selected = "AB")
leafletOutput("mymap", height = 415)

In the server function, I have multiple reactive expressions based on the input selected. This is how I've implemented the expressions.

 filtered_gender <- reactive({
   DF[DF$gender == input$checkbox_gender,]
 })

 filtered_hastv <- reactive({
   DF[DF$hasTV == input$radio_hastv,]
 })

 filtered_loc <- reactive({
   DF[DF$Location == input$loc,]
 })

I've already rendered the leaflet map. However, I'd like my map to change whenever all these three inputs are somehow selected. e.g. if a person selects, gender=Male, location = DE and hasTV=No, the appropriate map with the correct gps is plotted on the map.

So far, i'm only able to update leaflet map using one reactive expression as shown below.

 observe(leafletProxy("mymap", data = filtered_loc()) %>%
           clearMarkers()%>%
           addMarkers(radius =3)%>%
           label = ~htmlEscape(DF$hasTV)
         )  

How do I go about incorporating the other reactive expressions so that the map changes accordingly. Thanks.

1 Answer 1

3

You need to move all those filters in one reactive and use that in your leaflet plot -

library(dplyr)

filtered_data <- reactive({
   DF %>%
     filter(gender %in% input$checkbox_gender,
            hasTV %in% input$radio_hastv,
            Location %in% input$loc
            )
 })

 observe(leafletProxy("mymap", data = filtered_data()) %>%
           clearMarkers()%>%
           addMarkers(radius =3)%>%
           label = ~htmlEscape(hasTV) # note change; using DF here is wrong
         )
Sign up to request clarification or add additional context in comments.

5 Comments

I've implemented it and getting this error, Data contains 7 rows with either missing or invalid lat/lon values and will be ignored Warning: Error in filter_impl: Result must have length 140, not 0". Could the N/A's in Lat and Long be the issue?
When I change gender and hasTV to be only radiobuttons, it doesn't throw this error. It's a problem with checkboxGroupInput. Especially, when I deselect the two options.
I have edited the answer to use %in% instead of ==. If you deselect all checkbox choices then there is nothing to plot. Also try removing label argument in leafletProxy()...see if that is causing problems.
Thanks! That worked! I have another question, how does one add a reactive expression that will trigger different zoom levels of the map depending on the selected loc?
Not possible to answer that in a comment. You need to ask that as a separate question with a small reproducible example specific to that problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.