问题1 我尝试保存在此处使用RMAPS创建的传单地图,然后将其保存到HTML文件中。
L2 <- Leaflet$new()
L2$setView(c(29.7632836, -95.3632715), 10)
L2$tileLayer(provider = "MapQuestOpen.OSM")
L2
library(htmlwidgets)
saveWidget(L2,"t.html")
但是我有一个错误:
Error in envRefInferField(x, what, getClass(class(x)), selfEnv) :
‘width’ is not a valid field or method name for reference class “Leaflet”
答案1 多亏了Lukea,我们可以使用此语法:
leaflet(width = "100%") %>%
addProviderTiles("MapQuestOpen.OSM") %>%
setView(-95.3632715, 29.7632836, zoom = 10) -> L2
问题2 但是,如何添加附加组和settemplate:
# Add leaflet-heat plugin. Thanks to Vladimir Agafonkin
L2$addAssets(jshead = c(
"http://leaflet.github.io/Leaflet.heat/dist/leaflet-heat.js"
))
# Add javascript to modify underlying chart
L2$setTemplate(afterScript = sprintf("
<script>
var addressPoints = %s
var heat = L.heatLayer(addressPoints).addTo(map)
</script>
", rjson::toJSON(crime_dat)
))
L2
答案2 再次感谢Lukea,我们有答案:
L2$save(tf <- tempfile(fileext = ".html"),standalone=TRUE)
问题3
实际上,我最初的问题是我无法在rmarkDown中编写代码,以直接在HTML文档中生成地图。
现在我可以单独保存地图。但是如何将其集成到HTML文档中?Rapide解决方案是制作一个iframe。还有其他更优雅的解决方案吗?
尽管不是对您的问题的直接答案,但我建议另一种方法可以产生类似的结果:
library(leaflet)
library(htmlwidgets)
leaflet() %>%
addProviderTiles("MapQuestOpen.OSM") %>%
setView(-95.3632715, 29.7632836, zoom = 10) -> m
saveWidget(m, tf <- tempfile(fileext = ".html"))
或使用rMaps
:
library(plyr)
library(rCharts)
library(rMaps)
data(crime, package = 'ggmap')
crime_dat = ddply(crime, .(lat, lon), summarise, count = length(address))
crime_dat = toJSONArray(na.omit(unname(crime_dat)), json = F)
L2 <- Leaflet$new()
L2$setView(c(29.7632836, -95.3632715), 10)
L2$tileLayer(provider = "MapQuestOpen.OSM")
# Add leaflet-heat plugin. Thanks to Vladimir Agafonkin
L2$addAssets(jshead = c(
"http://leaflet.github.io/Leaflet.heat/dist/leaflet-heat.js"
))
# Add javascript to modify underlying chart
L2$setTemplate(afterScript = sprintf("
<script>
var addressPoints = %s
var heat = L.heatLayer(addressPoints).addTo(map)
</script>
", rjson::toJSON(crime_dat)
))
L2$save(tf <- tempfile(fileext = ".html"),standalone=TRUE)