R Shiny leaflet addPolygons (colors are not showing)

萝らか妹 提交于 2020-01-02 09:53:55

问题


I hope you can help me. I have created a choropleth Map with Leaflet. I merged my (dataframe with countries and a random score) and a Shapefile with the Polygon data. So far it is working, however if I implement it in R-Shiny, the map is showing, but with no color. There is also no error showing. Anyone knows why?

My code:

ui <- fluidPage(
  leafletOutput("map")
)


shinyServer(function(input, output) {

output$map <- renderLeaflet({
    test_map
  })
})

global.R

tmp <- tempdir()

url <- "http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/50m/cultural/ne_50m_admin_0_countries.zip"

file <- basename(url)

download.file(url, file)

unzip(file, exdir = tmp)

world <- readOGR(dsn = tmp, layer = "ne_50m_admin_0_countries", encoding = "UTF-8")

data <- data.frame(Code = c("AR", "AU", "BE", "BR"),
             Score = c(0.01, -0.05, 0.15, -0.22))

world <- merge(world, data,
               by.x = "iso_a2",
               by.y = "Code",
               sort = FALSE)

pal <- colorNumeric(
  palette = "RdYlGn",
  domain = world$Score
)

test_map <- leaflet(data = world) %>%
            addTiles() %>% 
            addPolygons(fillColor = ~pal(Score), 
                        fillOpacity = 0.9, 
                        color = "#BDBDC3", 
                        weight = 1)

回答1:


I know this is an old question and I'm not sure whether this will help or not, but I believe I had a similar problem to you which was just solved.

In my case, I had no trouble displaying polygon colours within Rstudio on my own PC, but certain web browsers and older versions of Rstudio refused to fill polygons with colours, even though all other aspects of the map worked fine.

The problem was that my colour palette consisted of a vector of hex codes with an alpha channel (the last couple of digits, specifying transparency). Removing the alpha channel from the hex codes solved my problem. It may be worth checking whether your colour vectors include alpha and if so, removing it with something like gsub(".{2}$","",your_colour_vector) as per the answer to my own problem (link above).

It doesn't look like your colours include alpha in your sample code but maybe it's a problem in your full code. That would explain why the sample code works but the full code doesn't. Might be something to look into anyway? Sorry I can't help more, I know this is a bit of a shot in the dark and not a full solution.



来源:https://stackoverflow.com/questions/37059404/r-shiny-leaflet-addpolygons-colors-are-not-showing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!