shiny, leaflet map, and dpylr::filter not playing nice (Error in : Problem with `filter()` input `..1`)

I'm creating a shiny app with a leaflet map and a text box underneath that responds to changes in the bounds of the map as the user navigates. Here's the basic reprex:

test_data <- data.frame(c(38.59222, 38.41083, 40.45917),
                        c(-89.09389, -87.88917, -87.76139), 
                        c("ice cream shop", "mom's house", "picnic shelter"))

names(test_data) <- c("latitude", "longitude", "comments")

ui <- fluidPage(
    
    # Application title
    titlePanel("My Town"),
        
        mainPanel(
#map of the town
            leafletOutput(outputId = "townmap"),
#comments textbox
            verbatimTextOutput("searchcomments")
        )
    )

server <- function(input, output) {
#the map    
    output$townmap <-
        renderLeaflet(
            {leaflet(test_data) %>% 
                    addTiles() %>%
                    addCircleMarkers(lng = ~longitude,
                                     lat = ~latitude)
            }
        )
#function to filter the comment data to match the bounds of the map    
    in_bounding_box <- function(data, boundsinput) {
        bounds <- boundsinput
        data %>%
            filter(latitude > bounds$south & latitude < bounds$north &
                       longitude < bounds$east & longitude > bounds$west)
    }
#making a text box with comments that responds to bounds changes in map    
    output$searchcomments <- renderPrint(
        in_bounding_box(test_data, input$townmap_bounds)$comments
    )
    
}

# Run the application 
shinyApp(ui = ui, server = server)


This code works (when you run the app and navigate so that one of the markers isn't visible, it's comment is filtered out of the text box). But it throws this error while the app is running:

Warning: Error in : Problem with `filter()` input `..1`.
x Input `..1` must be of size 3 or 1, not size 0.
ℹ Input `..1` is `&...`.
  116: <Anonymous>

My first swing at figuring this out was creating this sample data outside of the shiny app document to see if I was referring to input$townmap_bounds wrong (a named list per the documentation) or making a grammatical error with filter(). It worked smoothly, no errors - which has not helped me narrow down the problem at all, haha, except that it's not with the function of filter() or the way i'm referencing input$townmap_bounds.

test_data <- data.frame(c(38.59222, 38.41083, 40.45917),
                        c(-89.09389, -87.88917, -87.76139), 
                        c("ice cream shop", "mom's house", "picnic shelter"),
                        c("red", "blue", "yellow"))

names(test_data) <- c("latitude", "longitude", "comments", "color")

test_bounds <- list("north" = 39, "south" = 38, "east" = -87.8, "west" = -89.2)

m <- test_data %>%
  filter(latitude < test_bounds$north)

m

#no issue!

Would love input from some more experienced people.

guard your inputs with req()

    in_bounding_box(test_data, req(input$townmap_bounds))$comments
1 Like

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.