Dynamically render choropleth map with sliderInput in R shiny 2

I am trying to use the slider function to select and only display selected polygons using Shiny on a Leaflet map.

The code below gets the slider to react, but I want the colour scale to represent the depth, and the slider to select and display the energy polygons (removing non-relevant polygons) but with the depth colour scale. The polygon files contain both the depth and energy data.

The following question points me in the right direction. But I cannot get this to work with my data.

Shapefile wave-shp (wave.shp) on the following page: Downloads - ABPmer UK Renewables Atlas.

Here is my code:

library(shiny)
library(leaflet)
library(rgdal)
library(sf)
library(dplyr)
library(RColorBrewer)

wave_data <- read_sf("~/path/Wave.shp")

wave_data <- st_transform(wave_data, crs = '+proj=longlat 
+datum=WGS84')

## Load map

wave_data_map <- leaflet() %>% 
   addProviderTiles(providers$Esri.WorldTopoMap) %>% 
   setView(lng = -4.2026458, lat = 56.4906712, zoom = 5)
wave_data_map

bins <- c(0, 25, 50, 100, 150, 200, 300, 400, 500, 1000, 3100)
pal <- colorBin("RdYlBu", domain = wave_data$Ave_Depth, bins = 
bins)

## Add polygons

wave_data_map <- leaflet() %>% 
   addProviderTiles(providers$Esri.WorldTopoMap) %>% 
   setView(lng = -4.2026458, lat = 56.4906712, zoom = 5) %>% 
   addPolygons(data = wave_data, 
           weight = 1,
           smoothFactor = 0.5,
           color = "white", 
           fillOpacity = 0.5, 
           fillColor = pal(wave_data$Ave_Depth), 
           
)
wave_data_map

## Add legend

wave_data_map <- leaflet() %>% 
   addProviderTiles(providers$Esri.WorldTopoMap) %>% 
   setView(lng = -4.2026458, lat = 56.4906712, zoom = 5) %>% 
   addPolygons(data = wave_data, 
           weight = 1,
           smoothFactor = 0.5,
           color = "white", 
           fillOpacity = 0.3, 
           fillColor = pal(wave_data$Ave_Depth)) %>% 
   addLegend(pal = pal, 
         values = wave_data$Ave_Depth, 
         title = "Average depth",
         labFormat = labelFormat (suffix = "m"),
         opacity = 0.7, 
         position = "bottomright")
wave_data_map


# Define UI for application

ui <- bootstrapPage(
   tags$style(type = "text/css", "html, body 
                {width:100%;height:100%}"),
   leafletOutput("wave_data_map", width = "100%", height = 
                  "100%"),
   sliderInput("wave_data_slider", "Wave energy", 
                         min = 0, max = 75, 
                         value = c(min(wave_data$An_mn_P_OD), 
                         max(wave_data$An_mn_P_OD)), 
                         step = 5, 
                         round = 0.5,
                         dragRange = TRUE)
))


# Define server logic

server <- function(input, output, session) {
 wave_energy_output <- reactive ({
    wave_data %>% 
    filter(An_mn_P_OD >= input$wave_data_range[1]) %>% 
    filter(An_mn_P_OD <= input$wave_data_range[2]) 
 })
output$wave_data_map <- renderLeaflet(
  leaflet() %>% 
     addProviderTiles(providers$Esri.WorldTopoMap) %>% 
     setView(lng = -4.2026458, lat = 56.4906712, zoom = 5) %>% 
     addPolygons(data = wave_data, 
                 weight = 1,
                 smoothFactor = 0.5,
                 color = "white", 
                 fillOpacity = 0.3, 
                 fillColor = pal(wave_data$Ave_Depth)) %>% 
     addLegend(pal = pal, 
               values = wave_data$Ave_Depth, 
               title = "Average depth",
               labFormat = labelFormat (suffix = "m"),
               opacity = 0.7, 
               position = "bottomright")
)

observeEvent({input$wave_data_slider}, {
  leafletProxy("wave_data_map", data = wave_data) %>% 
     clearShapes() %>% 
     addPolygons(data = wave_energy_output,
        fillColor = pal, 
        weight = 0.0, 
        opacity = 1, 
        color = "white", 
        dashArray = 3, 
        fillOpacity = 0.7, 
     ) %>% clearControls() %>% 
     addLegend(pal = pal, 
               values = wave_data$Ave_Depth, 
               title = "Average depth",
               labFormat = labelFormat (suffix = "m"),
               opacity = 0.7, 
               position = "bottomright")
  
}
)}

# Run the application 

shinyApp(ui = ui, server = server)

Is the problem in observeEvent? When this runs I get all polygons in black, the slider does react to something, but does not return the required polygons.

Any help would really be appreciated.

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.