leaflet: Create a Leaflet web-map

View source: R/leaflet.R

leafletR Documentation

Create a Leaflet web-map

Description

Creates a web-map of users' spatial data over open base maps. Output consists of a ready to use HTML file (and a GeoJSON/TopoJSON data file).

Usage

leaflet(data, dest, title, size, base.map="osm", center, zoom, 
  style, popup, label, controls="all", incl.data=FALSE, overwrite=TRUE)
leaf(data, dest, title, size, base.map="osm", center, zoom, 
  style, popup, label, controls="all", incl.data=FALSE, overwrite=TRUE)

Arguments

data

Name(s) of data file(s) (GeoJSON/TopoJSON format), as string or a list of strings. Plotting order follows the file sequence.

dest

Path to the data file, as string. Optional – if missing, the current working directory is used.

title

Map title, as string. Default is "map".

size

Size of the map on the website in pixels, as numeric vector – c(width, height). Optional – if missing, a fullscreen (browser window) map is generated.

base.map

Base map(s) in the background of the data, as string. One or a list of "osm" (OpenStreetMap standard map), "tls" (Thunderforest Landscape), "mqosm" (MapQuest OSM), "mqsat" (MapQuest Open Aerial), "water" (Stamen Watercolor), "toner" (Stamen Toner), "tonerbg" (Stamen Toner background), "tonerlite" (Stamen Toner lite), "positron" (CartoDB Positron) or "darkmatter" (CartoDB Dark matter). Default is "osm". If base.map is a list, the last item is used as default base map and a layer control button is added to the map.

center

Map center coordinates in decimal degrees, as vector of two numeric values: c(latitude, longitude). Optional – if missing, the data layer is centered automatically. code has to be specified to use center.

zoom

Map zoom level, as integer value. Usually a value between 0 (global small scale) and 18 (detailed large scale). The MapQuest Open Aerial map (base.map="mqsat") provides only 12 zoom levels [0-11]. Optional – if missing, the zoom level is calculated for the bounding box of the data layer. center has to be specified to use zoom.

style

Style(s) of the data layer(s). One or a list of style object(s), created by styleSingle, styleGrad or styleCat. Optional – if missing, a default style is applied.

popup

Properties (attributes) of the data to be shown in a popup when a map object is clicked. String or a vector of strings. "*" adds all available properties to the popup. A list of (vectors of) strings specifies properties for multiple data layers. Per default no popups are shown.

label

Property (attribute) of the data to be shown in a dynamic label, as string. A list of strings specifies properties for multiple data layers. Per default no labels are shown. Only point data is supported and markers do not work very well.

controls

List of controls to be added to the map. Available controls are "zoom", "scale", "layer" and "legend". "all" (the default) adds all controls. Controls are only added if necessary, e.g. in case of one data layer there is no legend. NA omits all controls. Note: data layer controls only appear if incl.data is set to TRUE.

incl.data

If TRUE, data is included in the HTML file itself. Per default (incl.data=FALSE) the data is saved in a separate file. Including data in the HTML file allows for viewing the map locally on some browsers (e.g. Chrome and Opera).

overwrite

TRUE (which is the default) overwrites existing files with the same name.

Value

HTML file path, as string.

Note

data only accepts GeoJSON/TopoJSON files with one geometry type and geographical coordinates (longlat, WGS84).

Due to the security restrictions of some browsers (namely Chrome and Opera) it might be necessary to save the data inside the html file using incl.data = TRUE.

Author(s)

Christian Graul

References

Base map tiles are provided by

OpenStreetMap standard map http://www.openstreetmap.org
Thunderforest Landscape http://www.thunderforest.com
MapQuest OSM http://www.mapquest.com
MapQuest Open Aerial http://www.mapquest.com
Stamen Watercolor http://stamen.com
Stamen Toner http://stamen.com
Stamen Toner background http://stamen.com
Stamen Toner lite http://stamen.com
CartoDB Positron http://cartodb.com
CartoDB Dark matter http://cartodb.com

See Also

styleSingle, styleGrad, styleCat

Examples

## Not run: 
# prepare data
data(quakes)
dat <- toGeoJSON(data=quakes, dest=tempdir())

# create and view simple map
map <- leaflet(dat, dest=tempdir())
map  # redirects to browseURL(map)

# set output directory and map title
map <- leaflet(data=dat, dest=tempdir(), title="Fiji Earthquakes")

# set map size, center and zoom level
map <- leaflet(data=dat, dest=tempdir(), 
  size=c(800,600), center=c(-18.35, 179.75), zoom=6)

# set base map and popup/label
# magnitude is used as popup (type names(quakes) for available properties)
map <- leaflet(data=dat, dest=tempdir(), 
  base.map="mqsat", popup="stations", label="mag")
  
# minimalist? - no base map
map <- leaflet(data=dat, dest=tempdir(), 
  base.map=NA, popup="mag")

# include data in HTML file
map <- leaflet(dat, dest=tempdir(), incl.data=TRUE)

# preserve existing files from overwriting
map <- leaflet(dat, dest=tempdir(), overwrite=FALSE)

# more than one base map
map <- leaflet(data=dat, dest=tempdir(), 
  base.map=list("osm", "mqsat", "tls"))

# multiple properties in the popup
map <- leaflet(data=dat, dest=tempdir(), 
  popup=c("mag", "depth"))

# all available properties in the popup
map <- leaflet(data=dat, dest=tempdir(), 
  popup="*")

# change style
sty <- styleSingle(col="red", fill=NA)
map <- leaflet(data=dat, dest=tempdir(), base.map="mqsat", style=sty)

# controls
map <- leaflet(data=dat, dest=tempdir(), controls=NA)  # no controls
map <- leaflet(data=dat, dest=tempdir(), controls="scale")  # scale only
map <- leaflet(data=dat, dest=tempdir(), controls=c("zoom", "scale"))

# more than one data set
park <- system.file(package="leafletR", "files", "park_sk.geojson")
peak <- toGeoJSON(system.file(package="leafletR", "files", "peak_sk.kmz"), 
  dest=tempdir())  # httr package required
sty.1 <- styleSingle(col="green", fill="green")
sty.2 <- styleSingle(col="brown", fill="brown", rad=3)
map <- leaflet(data=list(park, peak), dest=tempdir(), 
  style=list(sty.1, sty.2), popup=list("*", "Name"))

# names in legend
# note: "_" and "." are replaced with blanks in the legend 
map <- leaflet(data=list(National_Parks=park, Peaks.above.600.m)=peak), 
  dest=tempdir(), style=list(sty.1, sty.2), popup=list("*", "Name"))

## End(Not run)

chgrl/leafletR documentation built on March 24, 2022, 7:53 a.m.