6

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?

1
  • 1
    This should be fixed with the latest htmlwidgets package. Try updating and/or installing from github. If you still get an error (I do not btw) then you can set the pandoc.stack.size option mentioned here: github.com/ramnathv/htmlwidgets/commit/…
    – hrbrmstr
    Commented Nov 28, 2015 at 18:30

2 Answers 2

7

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’
0
0

In case you do not mind about background maps, maybe you want to have a look at mapview (although the package does not support automated storing of created maps yet).

## required packages
library(mapview)
library(raster)

## get shapefile
FRA <- getData(country = "FRA", level = "0")

## create interactive map
m <- mapview(FRA)
m

france

For now, the best workaround to retrieve the created map is to locate the temporary folder where all the output from your mapview() call is stored. You can type tempfile() into the R console to figure out the general path to the temporary folders created from R (on my linux machine it looks like this - "/tmp/RtmpzMzJv7/file..."). There you should find a folder that was created by mapview() (they are usually called something like "viewhtml..." with "..." being some random string of numbers and letters). There you should find an "index.html" file and one or more folders. You can simply copy all contents to a folder of your liking and open "index.html" from there.

Unfortunately, function plainview - which creates maps without displaying background maps - currently supports 'Raster*' objects only, but we hope to implement a version for polygons (and all other kinds of shapefiles) soon.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.