In working with a map in shiny using mapview
, I've been flummoxed by reactives and trying to make my map dynamically update. Here is a reproducible example that doesn't work properly, despite being designed using principles from other SO answers:
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
library(sf)
library(mapview)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Test of Mapview Selective Viewing"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput("county", "County Name",
choices = c("All", levels(franconia$NAME_ASCI)),
selected = "All"
)
),
# Show a plot of the generated distribution
mainPanel(
mapviewOutput("mapPlot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
fran <- reactive({
f <- franconia
if(input$county != "All") f <- franconia %>% filter(NAME_ASCI == input$county)
f
})
output$mapPlot <- renderMapview({
#get the data
f <- isolate(fran())
#generate a map
mapview(f, zcol = "NAME_ASCI")
})
}
# Run the application
shinyApp(ui = ui, server = server)
This works, but the map will not update. I've tried putting in an action button - and putting input$button before the isolate statement, but, that causes the whole thing to throw an error.
I want to either see everything or a single county.
Any thoughts on what is missing/wrong here? I'm somewhat new to shiny and dealing with reactives!