如何访问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中使用以下文件路径将其打开:
This opens it up in Chrome with a filepath like this:
file:///var/folders/8x/v2tk5zy51x51jx9jbp0m29qr0000gn/T/RtmpQaeu1E/viewhtmlf74547061f7d/index.html.
假设我要将此发布到我的博客中.我究竟该如何访问该html文件?有没有办法设置保存到的位置?我以为它将保存到工作目录中,但事实并非如此.我想我可以通过终端访问它,但是我希望有一种更简单的方法.
Let's say I want to publish this to my blog. How exactly do I access this html file? Is there a way to set where it gets saved to? I assumed it would get saved to the working directory but that isn't the case. I guess I could access it via the terminal, but I'm hoping there's an easier way.
推荐答案
我开发了几个函数,可让您将传单地图保存在临时文件夹之外的其他地方.
I developed a couple of functions that lets you save a leaflet map somewhere other than a temp folder.
在此处查看要点: https://gist.github.com/barryrowlingson/d066a7ace15cf119681a 对于完整信息,简短版本是以下两个功能:
See the gist here: https://gist.github.com/barryrowlingson/d066a7ace15cf119681a for the full info, the short version is these two functions:
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
应该具有一组独立的地图文件.我认为它应该是可移植的,即可以在任何Web服务器上工作,但是我不能保证...
At that point the folder /wherever/you/want
should have a self-contained set of files for the map. I think it should be portable, ie work on any web server, but I can't guarantee that...
这篇关于如何访问R中的传单生成的地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!