Shiny Reactive Leaflet Plot not working

Hello all,

I am currently trying to make a Shiny App to visualize data in a leaflet plot. I let the user choose a dataset, which is inputted as a csv (so data frame), and then let them choose a column to plot. However, when I change the desired column, the plot doesn't change. Here is my code:


rm(list = ls())


shinyApp(ui = ui, server = server) # Run app


ui <- fluidPage(
  title = "OH PA Datasets",
  titlePanel("Visualizing Ohio and Pennsylvania Datasets"),
    selectInput("datasetname", "Select dataset:",
                list.files(paste(getwd(), "/data-oh", sep = "")) # List of datasets
    uiOutput("datasetcolumn2"), # Dropdown to select data to plot from dataset


server <- function(input, output, session) {
  # Allow user to choose column of dataset
  output$datasetcolumn2 <- renderUI({
    chosen_dataset <- read.csv(paste("data-oh/", input$datasetname, sep = ""))
    selectInput("datasetcolumn", "Data choice:",
                # Only allow user to plot numerical data
                Filter(function(x) class(chosen_dataset[,match(x, names(chosen_dataset))])=="numeric",
  # Map output for Ohio
  output$map_oh <- renderPlot({
    # Shapefile of all counties in USA
    shapefile <- st_read("us-county-boundaries/us-county-boundaries.shp")
    shapefile_oh <- shapefile[which(shapefile$state_name == "Ohio"),]
    chosen_dataset_oh <- read.csv(paste("data-oh/", input$datasetname, sep = ""))
    chosen_column <- input$datasetcolumn
    chosen_index <- match(chosen_column, names(chosen_dataset_oh))
    # Merge shapefile with desired data
    leaflet_oh <- merge(x = shapefile_oh, 
                        y = chosen_dataset_oh[,c(6,chosen_index)], 
                        by.x = "geoid", by.y = "FIPS", 
                        all.x = TRUE)
    pal <- colorNumeric(palette = "Blues", domain = chosen_dataset_oh[,chosen_index])
    names(leaflet_oh)[21] <- "X"
    output$map_oh <- renderLeaflet({
      leaflet() %>%
        addTiles() %>% 
        addPolygons(data = leaflet_oh,
                    stroke = FALSE,
                    smoothFactor = 0.2, 
                    fillOpacity = 1,
                    color = ~pal(X),
                    label = ~paste0(namelsad, "=", X)

The structure of my files is this:
Screenshot 2024-02-25 at 11.15.51 AM
The datasets I used are at CDC/ATSDR Social Vulnerability Index (SVI) , for the state of Ohio, in 2020, at the county level.

The shapefile I used is at US County Boundaries — Opendatasoft .

you have nested a renderLeaflet insider a renderPlot, and I dont think you should do that. You should make different renders distinct, if you need common objects that they share, pull those out to be reactives.

This topic was automatically closed 54 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.