我正在使用 R 中的传单构建地图,将其部署为 Shiny 应用程序。 Shiny 应用程序在 RStudio 中运行良好,但是当我在网络浏览器中打开它时,多边形失去了颜色。其他一切都很好,底图在那里,多边形都在那里,您可以将鼠标悬停在多边形上以查看信息等。唯一的变化是多边形从彩色变为全灰色。没有警告或错误消息。
我正在使用最新版本的 R (3.3.3) 和 Rstudio (1.0.136) 的 mac (Sierra),并且我的所有软件包都是最新的。我尝试过两种浏览器,结果相同(Chrome、Firefox)。特别奇怪的是,我尝试在 Windows 计算机上打开该应用程序,却遇到了相反的情况:Rstudio 中没有颜色,但网络浏览器 (Firefox) 中是全颜色的。
我猜测问题具体在于未能读取 addPolygons() 中的 fillColor 选项,但我不知道为什么此选项具体存在问题以及为什么它有时有效但其他无效。如果有人有任何想法,我很想听听!
PS,我认为我的问题类似于这个(未回答的)问题 https://stackoverflow.com/questions/37059404/r-shiny-leaflet-addpolygons-colors-are-not-showing但同样令人费解的是,在我的例子中,它可以在 Rstudio 中运行,但不能在 Web 浏览器中运行(或者在 Windows 机器上显然相反!)。
下面是一些代码(与我正在使用的数据集不同,为了清晰起见进行了修剪,但产生与上述完全相同的行为):
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)
Thanks!