rr-sfr-leafletchoropleth

Only the first polygon is showing up in Leaflet 2.0.1 choropleth plot


My leaflet choropleth plot in my Shiny App is only showing the first polygon after the recent package update to Leaflet 2.0.1.

I had adapted the code from the Leaflet for R webpage and it used to work.

Here is a MRE:

library(sf)
library(leaflet)

From http://leafletjs.com/examples/choropleth/us-states.js

states <- read_sf("~/Downloads/cb_2017_us_state_20m/cb_2017_us_state_20m.shp")

leaflet(states) %>%
  setView(-96, 37.8, 2) %>%
  addPolygons(color = "#444444", weight = 1, smoothFactor = 0.5,
              opacity = 1.0, fillOpacity = 0.5,
              fillColor = ~colorQuantile("YlOrRd", ALAND)(ALAND),
              highlightOptions = 
    highlightOptions(color = "white", 
                     weight = 2, bringToFront = TRUE, dashArray = ""))

The output with only the first polygon showing up:

Leaflet plot with only the first polygon showing up

I have checked the dataset and shapefile, and it looks correct, and can be successfully plotted using the plot function.


Solution

  • I found the solution, which was to assign dashArray = NULL. In the Leaflet for R webpage, the code had dashArray = "". Assigning an empty string to dashArray used to work as a default, but now it causes leaflet to only plot the first polygon.

    library(sf)
    library(leaflet)
    
    From http://leafletjs.com/examples/choropleth/us-states.js
    
    states <- read_sf("~/Downloads/cb_2017_us_state_20m/cb_2017_us_state_20m.shp")
    
    leaflet(states) %>%
      setView(-96, 37.8, 2) %>%
      addPolygons(color = "#444444", weight = 1, smoothFactor = 0.5,
              opacity = 1.0, fillOpacity = 0.5,
              fillColor = ~colorQuantile("YlOrRd", ALAND)(ALAND),
              highlightOptions = highlightOptions(color = "white", weight = 2,
                                                  bringToFront = TRUE,
                                                  dashArray = NULL
                                                  ))
    

    This outputs the correct plot:

    enter image description here