R Leaflet – Generate HTML File Using Leaflet in R

htmlleafletr

I try to display the France map on HTML with RStudio; I followed the tutorial to the World card in Creating an interactive map with Leaflet; Example works.

I wanted to do the same just by displaying the map of France:
code:

library(rgdal) 
library(foreign)
library(maptools)

FRA=readShapePoly("/Users/insa/Documents/exempleR/FRA_adm/FRA_adm0.shp")

#plot(FRA_adm0)
# Nettoyage des données

library(leaflet)
## Initialisation 
m <- leaflet(padding = 0)
## Ajout des pays

## Ajout des pays
m <- addPolygons(map = m, data = FRA, opacity = 100, 
                 color = "#FAFCFA", 
                 weight = 0.25,popup = NULL,
                 options = list(clickable = FALSE), 
                 fill = T, fillColor = "#B3C4B3", 
                 fillOpacity = 100)

## Dimensions de la carte
m$width <- 874
m$height <- 700


# Export de la carte en html
library(htmlwidgets)
saveWidget(m, 'mapfrance.html', selfcontained = TRUE)

error:

Stack space overflow: current size 33632 bytes.
Use `+RTS -Ksize -RTS' to increase it.
Erreur : pandoc document conversion failed with error 2

I do not understand this error, is that it's the input file is shapefile .shpaand not .rdata?

Best Answer

Try to set the selfcontained variable to FALSE. That worked for me. Beside that make sure that the Shape of France is saved with the coordinate system WGS 84 (EPSG:4326).

# install packages
install.packages(c("rgdal", "maptools", "leaflet", "htmlwidgets"), dependencies = TRUE)

# load libraries
library("rgdal")
library("maptools")
library("leaflet")
library("htmlwidgets")

# load france as shape
FRA=readShapePoly("data/france.shp")

# Initialsation
m <- leaflet(padding = 0)

# Add country
m <- addPolygons(map = m, data = FRA, opacity = 100, 
                 color = "#FAFCFA", 
                 weight = 0.25,popup = NULL,
                 options = list(clickable = FALSE), 
                 fill = T, fillColor = "#B3C4B3", 
                 fillOpacity = 100)

# Dimention of the map
m$width <- 874
m$height <- 700


# Export as HTML file
saveWidget(m, 'mapfrance.html', selfcontained = FALSE)

leaflet france

EDIT

After the hint of @hrbrmstr above I check my package version:

packageVersion("htmlwidgets")
[1] ‘0.5’