9

Let's 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 up in Chrome with a filepath like this:

file:///var/folders/8x/v2tk5zy51x51jx9jbp0m29qr0000gn/T/RtmpQaeu1E/viewhtmlf74547061f7d/index.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.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
ytk
  • 2,787
  • 4
  • 27
  • 42

4 Answers4

17

I developed a couple of functions that lets you save a leaflet map somewhere other than a temp folder.

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"))
}

then all you do is:

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

or in your mode of working:

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

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

Spacedman
  • 92,590
  • 12
  • 140
  • 224
13

a late response:

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

einar

ytk
  • 2,787
  • 4
  • 27
  • 42
einar
  • 2,575
  • 1
  • 16
  • 14
3

Once you opened your map in chrome (or any other browser) you can simply go to "File -> Save as" and then save the .html page to a 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 webmap with R or Rmarkdown.


Of course you can also upload your file to Rpubs.com by clicking on the "Publish" button, when you create your map with Rmarkdown. From there you can easily access it via a provided link.

maRtin
  • 6,336
  • 11
  • 43
  • 66
  • I tried this and it completely messed up the map. For instance, in the original map, I can zoom in, zoom out, etc., but in the new map, I cannot. Also, for some weird reason, it got all jumbled up. The top right part was in center, the center part was at the bottom left, and everything was moved around in a seemingly random way, it looks nothing like the original. Any idea why this is happening? – ytk May 19 '15 at 22:14
  • 1
    can`t really think of a reason why you are facing these problems. I tried uploading your code to rpubs and it works fine: http://rpubs.com/SmaRtin/so-question – maRtin May 19 '15 at 22:19
  • 1
    That sounds like the CSS for the tiles hasn't been loaded. Browser "save as" functionality does sometimes get this wrong. – Spacedman May 19 '15 at 22:30
  • @maRtin I was referring to saving it as a .html page. It works fine if I upload it to rpubs! – ytk May 19 '15 at 23:06
1

When I try to install a package named "leaflet", the dialog with CRAN only shows a package named leafletR. Installing and loading that package does succeed with a message to the console:

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

And that map has the desired functionality. Given the amount of information that I can access from the web browser, I'm guessing that I'm actually interfaced through Chrome to an OpenStreetMap server rather than interacting with a disk file data service.

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

Further searching shows this to be only hosted on RStudio and not on CRAN: http://robinlovelace.net/r/2015/02/01/leaflet-r-package.html

I needed a fresh session since I was getting error that I suspected were cause by having both leaflet and leafletR loaded at the same time. In my browser I left-clicked to bring up a ViewSource window and then selected and copy below. Both Chrome and Firefox have the capacity to display the underlying code and support selection and copying to an 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>

The code alone is not enough. The rest of the required support files will be saved in a directory with the same name as the html file and the browser "Save As ..." function is best for that:

enter image description here

IRTFM
  • 258,963
  • 21
  • 364
  • 487