Leaflet polygons lose color when R Shiny opens in a web browser

I am building a map using flyers in R, which will be deployed as a Shiny application. The Shiny application works fine in RStudio, but when I open it in a web browser, the polygons lose their color. Everything else is in order, there is a basic map, all polygons are there, you can hang over polygons to see information, etc. The only change is that polygons go from colorful to all gray. No warnings or error messages.

I am working on mac (Sierra) with the latest versions of R (3.3.3) and Rstudio (1.0.136) and all my packages have been updated. I tried two browsers with the same result (Chrome, Firefox). What is especially strange is that I tried to open the application on a computer running Windows and faced the opposite situation: in Rstudio there are no colors, but full-color in a web browser (Firefox).

I suppose the problem is that it was not possible to read the fillColor parameter in addPolygons (), but I have no idea why there is a problem with this option and why it works sometimes, but not others. I would love to hear this if anyone has any ideas!

PS, I think my problem is similar to this (unanswered) question , but again, it puzzles me that in my case it works in Rstudio, but not in a web browser (or vice versa on Windows machines, obviously!).

Here is the code below (another dataset from the one I use and trimmed for clarity, but produces exactly the same behavior as described above):

library(leaflet)
library(rgdal)
library(shiny)

server <- function(input,output){

  output$map <- renderLeaflet({

    # Example data (borrowed from a tutorial at https://rpubs.com/walkerke/leaflet_choropleth)
    tmp <- tempdir()
    url <- "http://personal.tcu.edu/kylewalker/data/mexico.zip"
    file <- basename(url)
    download.file(url, file)
    unzip(file, exdir = tmp)
    mexico <- readOGR(dsn = tmp, layer = "mexico", encoding = "UTF-8")

    leaflet(mexico) %>%
      addTiles() %>%
      addPolygons(weight = 1.2, label = ~name,
                  fillColor = topo.colors(4), fillOpacity = .5,
                  highlightOptions = highlightOptions(color = "black", weight = 2, bringToFront = TRUE, fillOpacity = .8)) %>%
      addProviderTiles("Esri.WorldPhysical")
  })
}

ui <- fluidPage(

  titlePanel("A map"),

  sidebarLayout(
    sidebarPanel("options go here"),
    mainPanel(
      leafletOutput("map", height = 600)
    )
  )
)

shinyApp(ui = ui, server = server)

Thank!

+4
source share
1 answer

topo.colors returns the hexadecimal representation of colors with an alpha channel.

You can remove part of the alpha channel by doing:

gsub(".{2}$","",topo.colors(4))

Not sure if the RStudio viewer can handle alpha, not chrome or firefox.

Your leaflet could be:

leaflet(mexico) %>%
      addTiles() %>%
      addPolygons(weight = 1.2, label = ~name,
                  fillColor = gsub(".{2}$","",topo.colors(4)), fillOpacity = .5,
                  highlightOptions = highlightOptions(color = "black", weight = 2, bringToFront = TRUE, fillOpacity = .8)) %>%
      addProviderTiles("Esri.WorldPhysical")
+1
source

Source: https://habr.com/ru/post/1672425/


All Articles