Jay*_*M-C 5 r geojson rstudio leaflet shiny
我正在使用R中的传单构建地图,以部署为Shiny应用程序.Shiny应用程序在RStudio中运行良好,但是当我在Web浏览器中打开它时,多边形会失去颜色.其他一切都很好,基本地图就在那里,多边形都在那里,你可以将鼠标悬停在多边形上以查看信息等.唯一的变化是多边形从彩色变为灰色.没有警告或错误消息.
我正在使用最新版本的R(3.3.3)和Rstudio(1.0.136)的mac(Sierra),我的所有软件包都是最新的.我尝试了两种结果相同的浏览器(Chrome,Firefox).特别奇怪的是,我尝试在Windows机器上打开应用程序并遇到相反的情况:Rstudio中没有颜色,而是Web浏览器中的全彩色(Firefox).
我猜这个问题是专门没有在addPolygons()中读取fillColor选项,但我不知道为什么这个选项有问题,为什么它有时会工作而不是其他工作.如果有人有任何想法,我很乐意听到它!
PS,我认为我的问题类似于这个(未答复的)问题,但同样,令人费解的是,在我的情况下,它适用于Rstudio但不适用于网络浏览器(或者显然在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)
Run Code Online (Sandbox Code Playgroud)
谢谢!
topo.colors返回带有 alpha 通道的颜色的十六进制表示形式。
您可以通过执行以下操作删除 Alpha 通道部分:
gsub(".{2}$","",topo.colors(4))
Run Code Online (Sandbox Code Playgroud)
不知道为什么 RStudio 查看器窗格可以处理 alpha,而不是 chrome 或 firefox。
您的传单电话可能是:
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")
Run Code Online (Sandbox Code Playgroud)