Reputation: 1
I'm making a map using Leaflet and Shiny, but have a problem when it comes to the reactive section of my code.
I would like to use a dropdown filter to select just the points within a particular area. Currently, the following code filters points down to the correct area, but - weirdly - they're not the points that should be there.
For example, if I select Area 1, then only points in Area 1 are shown, but they're points from all over the map that shouldn't actually be in Area 1.
I've identified what I think is the problematic section below.
library(leaflet)
library(shiny)
library(dplyr)
dairypoints <- read.csv("Data dairies.csv")
rdmareas <- readOGR("The New Dairy Areas.shp")
ui <- fluidPage(
titlePanel("Search Dairy Producers in the UK by Area"),
selectInput("dropdown", "Select Area", choices = c(dairypoints$area), multiple = FALSE),
leafletOutput("dairypointsmap", height = 1000, width= 1000)
)
pal <- colorFactor(c("blue", "black"), domain = c("Dairy Farm", "RDM Farm"))
#I think it's this section (next 3 lines) that's causing the problem
server <- function(input, output){
filtered <- reactive({
dairypoints %>% filter(dairypoints$area==input$dropdown)
})
output$dairypointsmap <- renderLeaflet({
dairypointsmap <- leaflet(filtered()) %>%
setView(lng = -2.916976, lat = 54.815864, zoom = 6) %>%
addTiles() %>%
addLegend("topright", pal = pal, values = ~Type) %>%
addPolygons(data = rdmareas,
stroke = TRUE, fillOpacity = 0,
smoothFactor = 0.1, color = "black", opacity = 1, weight = 1) %>%
addCircleMarkers(
radius = 4,
color = ~pal(dairypoints$Type),
stroke = FALSE, fillOpacity = 1,
popup = paste("Name", dairypoints$Name, "<br>",
"Producer ID", dairypoints$ProducerID, "<br>",
"Area Number", dairypoints$area, "<br>",
"Post Code", dairypoints$Postcode, "<br>",
"CPH", dairypoints$CPH, "<br>",
"Farm Type", dairypoints$Type))
})
options(shiny.sanitize.errors = FALSE)
}
shinyApp(ui = ui, server = server)
Upvotes: 0
Views: 161