0
votes

I am trying to create a Shiny Leaflet map with slider input based on the years listed in the columns. The data component of the Large SpatialPolygonsDataFrame looks like this with the postcode on the side and years as column names: enter image description here

I am wanting to create a slider using the P2015 to P2020 columns.

How do I get the map to change the colours when a different input year is selected?

I'm not sure I understand how to use the reactive function properly.

Here is the code that I currently have:

ui <- fillPage(
  
  titlePanel("Title"),
  
  tags$style(type = "text/css", "html, body {width:100%; height:100%}"),
  
  leafletOutput("mymap", width = "100%", height = "100%"),
  
  absolutePanel(top = 10, right = 10,
                sliderInput("year", "Year", min = 2015, max = 2020,
                            value = 2015, step = 1)
  )
)

server <- function(input, output, session) {

  LargeSpatialPDF <- rgdal::readOGR("~/blah.geojson")

  output$mymap <- renderLeaflet({
    leaflet(LargeSpatialPDF ) %>% 
      addMapPane(name="polygons", zIndex = 410) %>% 
      setView( lat=-32.30, lng=116.5 , zoom=9.45) %>%
      addProviderTiles(providers$Esri.WorldGrayCanvas) %>% 
      addProviderTiles(providers$Stamen.TonerLabels, 
                       options = leafletOptions(pane = "maplabels"),
                       group = "map labels")
  })

#not sure how to use this reactive statement here?
  layer <- reactive({LargeSpatialPDF})

observeEvent({input$year}, {
    year_column <- paste0('P',input$year)
    data=layer()[year_column]
  
    bins <- c(0,1,5, 10,15,20,25,30,Inf)
    pal <- colorBin(c("#fff7cf",
                      "#f7e2af",
                      "#f2cc91",
                      "#eeb576",
                      "#eb9c60",
                      "#e7824e",
                      "#e36543",
                      "#dd433d",
                      "#d6003d"), domain = LargeSpatialPDF@data[year_column], bins = bins)
    
    leafletProxy("mymap", data = data) %>%
      addPolygons(
        fillColor = ~pal(x),
        weight = 1,
        opacity = 1,
        color = "white",
        dashArray = "3",
        fillOpacity = 0.7,
        highlight = highlightOptions(
          weight = 2,
          color = "white",
          dashArray = "",
          fillOpacity = 1,
          bringToFront = TRUE),
        label = labels,
        labelOptions = labelOptions(
          style = list("font-weight" = "normal", padding = "3px 8px"),
          textsize = "15px",
          direction = "auto"))
      
  })
  
}

shinyApp(ui = ui, server = server)

1

1 Answers

1
votes

reactive isn't necessary because LargeSpatialPDF is static.
I think the problems of your code are:

  1. Whrere does x come from in fillColor = ~pal(x) ??
  2. not df["colname"] but df[["colname"]] gives a vector.
  3. clearShapes() is necessary.

Below is my example:

library(shiny)
library(leaflet)
library(sp)

ui <- fillPage(

    titlePanel("Title"),
    
    leafletOutput("mymap", width = "100%", height = "100%"),
    
    absolutePanel(top = 10, right = 10,
                  sliderInput("year", "Year", min = 1, max = 3,
                              value = 1, step = 1)
    )
)


server <- function(input, output, session) {
    # sample_data
    dsn <- system.file("vectors/ps_cant_31.MIF", package = "rgdal")[1]
    LargeSpatialPDF <- rgdal::readOGR(dsn=dsn, layer="ps_cant_31", stringsAsFactors=FALSE)
    set.seed(1); LargeSpatialPDF@data <- cbind(LargeSpatialPDF@data, 
                                               data.frame(P1 = sample(44), P2 = sample(44), P3 = sample(44)))
    
    output$mymap <- renderLeaflet({
        leaflet() %>% 
            addMapPane(name="polygons", zIndex = 410) %>% 
            setView( lat=43.5, lng=1.5 , zoom=8 ) %>%
            addProviderTiles(providers$Esri.WorldGrayCanvas) %>% 
            addProviderTiles(providers$Stamen.TonerLabels, 
                             options = leafletOptions(pane = "maplabels"),
                             group = "map labels")
    })
    
    observeEvent({input$year}, {
        year_column <- paste0('P',input$year)
        
        bins <- seq(0, 45, length = 9)
        pal <- colorBin(c("#fff7cf",
                          "#f7e2af",
                          "#f2cc91",
                          "#eeb576",
                          "#eb9c60",
                          "#e7824e",
                          "#e36543",
                          "#dd433d",
                          "#d6003d"), domain = LargeSpatialPDF@data[[year_column]], bins = bins)
        
        leafletProxy("mymap") %>%
            clearShapes() %>%   # important
            addPolygons(
                data = LargeSpatialPDF,
                fillColor = ~ pal(LargeSpatialPDF@data[[year_column]]),   # use values of the year
                options = pathOptions(pane = "polygons"))  # my guess
    })
}

shinyApp(ui = ui, server = server)