Saving leaflet output as html

32,377

Solution 1

Something like:

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

seems to work on most widgets.

Solution 2

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.

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

Solution 4

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.

Share:
32,377

Related videos on Youtube

h.l.m
Author by

h.l.m

Updated on July 09, 2022

Comments

  • h.l.m
    h.l.m almost 2 years

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

  • RockScience
    RockScience over 7 years
    Error: pandoc document conversion failed with error 67
  • einar
    einar over 7 years
    @RockScience: A likely reason may a bug in the development version of leaflet. try install.packages("leaflet") and then rerun the code.
  • RockScience
    RockScience over 7 years
    @einar indeed I have been using install_github("RStudio/leaflet") as I need some of the functionalities only available there...