我有一个传单地图,我已经修改,有一个特殊的CSS弹出下面的建议here。现在,我想将结果保存为HTML网页。结果对象的类型是shiny.tag.list
,我可以用查看器查看它,并使用Export->Save as web page
按钮手动将其导出到HTML.然而,我找不到R代码来做同样的操作。我已经尝试了mapview
包中的mapshot
函数和htmltools
中的saveWidget
函数,但它们都失败了,并显示以下错误:
> saveWidget(lf,"test.html")
Error in .getNamespace(pkg) :
invalid type/length (symbol/0) in vector allocation
我还尝试过htmltools
包的save_html
函数,但这会生成一个非独立的HTML文件。
我该如何做Export->Save as web page
按钮对R代码所做的事情?
以下是演示这些问题的一些示例代码:
library(sf)
library(leaflet)
library(htmltools)
library(mapview)
x = data.frame(lat=c(44,45),lon=c(3,2),
label=c("p1","p2"))
x = st_as_sf(x,coords=c("lon","lat"),crs=4326)
lf = x %>% leaflet %>% addTiles() %>% addMarkers(label=~label,popup=~label)
st = browsable(
tagList(list(
tags$head(
tags$style(".leaflet-popup-content-wrapper {background-color: #ff0000;}")
),
lf
))
)
saveWidget(st,"test.html") # Fails with error
mapshot(st,"test.html") # Fails with same error
save_html(st,"test.html") # Produces HTML with external dependencies
发布于 2020-08-05 04:48:33
我遇到了同样的问题,并用htmlwidgets::prepandContent
解决了它
library(sf)
library(leaflet)
library(htmltools)
library(mapview)
library(htmlwidgets)
x = data.frame(lat=c(44,45),lon=c(3,2),
label=c("p1","p2"))
x = st_as_sf(x,coords=c("lon","lat"),crs=4326)
lf = x %>% leaflet %>% addTiles() %>% addMarkers(label=~label,popup=~label)
st = lf %>% prependContent(
tags$head(
tags$style(".leaflet-popup-content-wrapper {background-color: #ff0000;}")
)
)
saveWidget(st,"test.html") # works!
https://stackoverflow.com/questions/61058110
复制相似问题