How to access a map created by a sheet in R

Say I have a code like this

# 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 

This opens it in Chrome with the following file path:

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

Say I want to post this on my blog. How can I access this html file? Is there any way to establish where it will be saved? I assumed that it will be saved in the working directory, but it is not. I think I could access it through the terminal, but I hope that there will be an easier way.

+6
source share
4 answers

I have developed several functions that allow you to save a flyer map somewhere other than a temporary folder.

See the gist: https://gist.github.com/barryrowlingson/d066a7ace15cf119681a for full details, the short version are 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")) } 

then all you do is:

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

or in your mode of operation:

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

At this point, the /wherever/you/want folder should have an offline set of files for the map. I think that it should be portable, that is, work on any web server, but I can not guarantee that ...

+15
source

late answer:

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

Einar

+8
source

Once you have opened your map in chrome (or in any other browser), you can simply go to "File -> Save as" and then save the .html page to the target directory under a user-defined name. (Or just press cmd + s on mac or ctrl + s on windows.) This is usually what I do when I create a web map with R or Rmarkdown.


Of course, you can also download the Rpubs.com file by clicking the Publish button when you create a map using Rmarkdown. From there you can easily access it using the link provided.

+3
source

When I try to install a package named leaflet, the CRAN dialog only shows a package named leafletR . Installing and downloading this package is performed with a message to the console:

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

And this card has the desired functionality. Given the amount of information that I can get in a web browser, I assume that I actually interact with Chrome on the OpenStreetMap server, and not interact with the data transfer service on disk.

In the version downloaded from CRAN, there is no addTiles function. And using sos :: findFn does not find it in any other package. This may be a new feature available only in the github version: https://github.com/chgrl/leafletR

A further search shows that this placement is only on RStudio, and not on CRAN: http://robinlovelace.net/r/2015/02/01/leaflet-r-package.html

I needed a new session, because I was getting an error, which, as I suspected, was caused by the fact that both leaflets and leaflets were loading at the same time. In my browser, I left-clicked to open the ViewSource window, and then select and copy below. Both Chrome and Firefox can display the base code and support selection and copying to the editor.

 <!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> 

One code is not enough. The remaining necessary support files will be saved in the directory with the same name as the html file, and the "Save As ..." function for the browser is best suited:

enter image description here

+1
source

Source: https://habr.com/ru/post/987528/


All Articles