如何访问R中传单生成的地图

How to access map generated by leaflet in R

假设我有这样的代码

# Install devtools if needed
if(!require(devtools)) install.packages("devtools")
# view rawif-devtools.R hosted with ❤ by GitHub
# Install leaflet package
if(!require(leaflet)) install_github("rstudio/leaflet")
library("leaflet")
mymap <- leaflet()
mymap <- addTiles(mymap)
mymap

这会在 Chrome 中使用如下文件路径打开它:

file:///var/folders/8x/v2tk5zy51x51jx9jbp0m29qr0000gn/T/RtmpQaeu1E/viewhtmlf74547061f7d/index.html. 

假设我想将其发布到我的博客。我该如何访问这个 html 文件?有没有办法设置保存到的位置?我以为它会被保存到工作目录,但事实并非如此。我想我可以通过终端访问它,但我希望有更简单的方法。

在 chrome(或任何其他浏览器)中打开地图后,您只需转到 "File -> Save as",然后将 .html 页面保存到用户下的目标目录定义的名称。 (或者只需在 mac 上按 cmd + s 或在 windows 上按 ctrl + s。) 当我使用 R 或 Rmarkdown 创建网络地图时,我通常会这样做。


当然,当您使用 Rmarkdown 创建地图时,您也可以通过单击 "Publish" 按钮将文件上传到 Rpubs.com。从那里您可以通过提供的 link.

轻松访问它

我开发了几个功能,可让您将传单地图保存在临时文件夹以外的其他地方。

请在此处查看要点:https://gist.github.com/barryrowlingson/d066a7ace15cf119681a 了解完整信息,简短版本是这两个函数:

saveas <- function(map, file){
    class(map) <- c("saveas",class(map))
    attr(map,"filesave")=file
    map
}

print.saveas <- function(x, ...){
    class(x) = class(x)[class(x)!="saveas"]
    htmltools::save_html(x, file=attr(x,"filesave"))
}

那么你要做的就是:

leaflet() %>% etc etc %>% saveas("/wherever/you/want/index.html")

或您的工作方式:

mymap <- leaflet()
mymap <- addwhatever(mymap)
saveas(mymap, "/wherever/you/want/index.html")

此时文件夹 /wherever/you/want 应该有一组独立的地图文件。我认为它应该是可移植的,即可以在任何网络服务器上工作,但我不能保证...

当我尝试安装名为 "leaflet" 的包时,与 CRAN 的对话框仅显示名为 leafletR 的包。安装和加载该包确实成功并向控制台显示一条消息:

 Your leaflet map has been saved under /Users/myuser_name/map/map.html

并且该地图具有所需的功能。考虑到我可以从 Web 浏览器访问的信息量,我猜我实际上是通过 Chrome 连接到 OpenStreetMap 服务器,而不是与磁盘文件数据服务交互。

CRAN下载的版本没有addTiles功能。并且使用 sos::findFn 在任何其他包中都找不到它。这可能是 github 版本才有的新功能: https://github.com/chgrl/leafletR

进一步搜索显示这仅托管在 RStudio 而不是 CRAN 上:http://robinlovelace.net/r/2015/02/01/leaflet-r-package.html

我需要一个新的会话,因为我收到了错误,我怀疑是由于同时加载了 leaflet 和 leafletR。在我的浏览器中,我左键单击以调出一个 ViewSource window,然后选择并复制到下面。 Chrome 和 Firefox 都可以显示底层代码并支持选择和复制到编辑器。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<script src="lib/htmlwidgets-0.3.2/htmlwidgets.js"></script>
<script src="lib/jquery-1.11.1/jquery.min.js"></script>
<link href="lib/leaflet-0.7.3/leaflet.css" rel="stylesheet" />
<script src="lib/leaflet-0.7.3/leaflet.js"></script>
<link href="lib/leafletfix-1.0.0/leafletfix.css" rel="stylesheet" />
<script src="lib/leaflet-binding-0.0.16/leaflet.js"></script>

</head>
<body style="background-color:white;">
<div id="htmlwidget_container">
  <div id="htmlwidget-3689" style="width:100%;height:400px;" class="leaflet"></div>
</div>
<script type="application/json" data-for="htmlwidget-3689">{ "x": {
 "calls": [
 {
 "method": "addTiles",
"args": [
 "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
null,
{
 "minZoom":                 0,
"maxZoom":                18,
"maxNativeZoom": null,
"tileSize":               256,
"subdomains": "abc",
"errorTileUrl": "",
"tms": false,
"continuousWorld": false,
"noWrap": false,
"zoomOffset":                 0,
"zoomReverse": false,
"opacity":                 1,
"zIndex": null,
"unloadInvisibleTiles": null,
"updateWhenIdle": null,
"detectRetina": false,
"reuseTiles": false,
"attribution": "&copy; <a href=\"http://openstreetmap.org\">OpenStreetMap</a> contributors, <a href=\"http://creativecommons.org/licenses/by-sa/2.0/\">CC-BY-SA</a>" 
} 
] 
} 
] 
},"evals": [  ] }</script>
<script type="application/htmlwidget-sizing" data-for="htmlwidget-3689">{ "viewer": {
 "width": "100%",
"height":               400,
"padding":                 0,
"fill": true 
},"browser": {
 "width": "100%",
"height":               400,
"padding":                 0,
"fill": true 
} }</script>
</body>
</html>

仅有代码是不够的。其余所需的支持文件将保存在与 html 文件同名的目录中,浏览器 "Save As ..." 功能最适合:

延迟回复:

library(leaflet)
mymap <- leaflet() %>%
  addTiles()
library(htmlwidgets)
saveWidget(widget = mymap, file = "/wherever/you/want/mymap.html")

艾纳尔