48

I am using RStudio to create some some leaflet images.

I would like to be able to save the output as an HTML so that it can be emailed and others can view it.

Below is some sample R code which was taken from [here] to create a sample leaflet image.

devtools::install_github('rstudio/leaflet')
library(leaflet)
rand_lng = function(n = 10) rnorm(n, -93.65, .01)
rand_lat = function(n = 10) rnorm(n, 42.0285, .01)
m = leaflet() %>% addTiles() %>% addCircles(rand_lng(50), rand_lat(50), radius = runif(50, 10, 200))
m

Any code to be able to the output as HTML would be much appreciated...

h.l.m
  • 13,015
  • 22
  • 82
  • 169
  • `saveWidget` is the way to go (as user1825941answered). if you want to make bitmaps, https://github.com/tesseradata/trelliscope/blob/master/R/thumb.R is the other way to go. – hrbrmstr Jul 26 '15 at 22:00

6 Answers6

89

Something like:

library(htmlwidgets)
saveWidget(m, file="m.html")

seems to work on most widgets.

sieste
  • 8,296
  • 3
  • 33
  • 48
einar
  • 2,575
  • 1
  • 16
  • 14
  • Error: pandoc document conversion failed with error 67 – RockScience Nov 14 '16 at 10:55
  • 1
    @RockScience: A likely reason may a bug in the development version of leaflet. try install.packages("leaflet") and then rerun the code. – einar Nov 15 '16 at 20:58
  • @einar indeed I have been using install_github("RStudio/leaflet") as I need some of the functionalities only available there... – RockScience Nov 16 '16 at 03:35
12

Open a new RMarkdown document. When you are using RStudio go to File -> New File -> R Markdown. Once you saved the file, you can insert your code into a chunk, like this:

---
title: "Leaflet Map"
output: html_document
---

```{r}
library(leaflet)
rand_lng = function(n = 10) rnorm(n, -93.65, .01)
rand_lat = function(n = 10) rnorm(n, 42.0285, .01)
m = leaflet() %>% addTiles() %>% addCircles(rand_lng(50), rand_lat(50), radius = runif(50, 10, 200))
m
```

Then Press the Knit HTML Button above the code window and your application will open in a new HTML file. You can send the file via eMail or upload it to your ftp.

maRtin
  • 6,336
  • 11
  • 43
  • 66
3

I have faced the same problem and after installing Github version the problem was fixed.

# Or Github version
if (!require('devtools')) install.packages('devtools')
devtools::install_github('rstudio/leaflet')

My present version is 1.1.0.9000, running on macOS Sierra, RStudio Version 1.1.232 and R 3.4.0

You can export from RStudio or save using htmlwidgets.

Clemsang
  • 5,053
  • 3
  • 23
  • 41
RgrNormand
  • 530
  • 4
  • 10
2

Another option using mapview library is:

library(mapview) mapshot(m, url = "m.html")

Note that you can also set the output to .png, .pdf, or .jpeg.

trevi
  • 589
  • 1
  • 6
  • 11
  • I get this error when I try to save it as png. Do you know why this is? `[WARNING] Could not deduce format from file extension .png. Defaulting to html` – 89_Simple Aug 31 '22 at 21:29
0

library(mapview)

To save as a "png" or "jpg" image:

mapshot(m, file = "m.png")
mapshot(m, file = "m.jpeg")

Even pdf can be used

David
  • 5,882
  • 3
  • 33
  • 44
0

Both solutions saveWidget or mapshot work correctly (saveWidget seems to be quicker), however, you should take care with color selection, especially in those choosed for borders/lines of polygons because in the stored map not all colours in borders are drawn ("grey50" for example is ignored while pure colours as "black" are drawn normally).

Curiously, these colours are stored and shown correctly when they are used as fill colour.