knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) library(leaflet) library(leafsync)
library(smartmap)
smartmap produces interactive maps from a variety of R objects with minimal coding required. This makes it great for previewing spatial data. It provides only three functions:
smap()
: The core function. Smartly create leaflet
maps from R objects. Maps created with smap()
contain two dialogues in the top
right corner: one for switiching the background map and one for measuring
distances and areas.smart_as_sf()
: Applies the same heuristics as smap()
, but returns
sf objects instead.as_coord_matrix
: Applies the same heuristics as smap()
, but returns a
numeric matrix with longitude and latitude instead.Viewing data.frames
with longitude and latitude columns was the basic use case
for which smartmap was created. The heuristics for automatically determining
the geo-coordinate columns are simple, but should work for most use
cases.
cities <- data.frame( city = c("Bregenz", "Eisenstadt", "Wiener Neustadt", "Graz", "Klagenfurt", "Linz", "Salzburg", "Innsbruck", "Vienna"), LaTituDE = c(47.51669707, 47.83329908, 47.81598187, 47.0777582, 46.62034426, 48.31923281, 47.81047833, 47.28040733, 48.20001528), lng = c(9.766701588, 16.53329747, 16.24995357, 15.41000484, 14.3100203, 14.28878129, 13.0400203, 11.4099906, 16.36663896), pop = c(26928, 13165, 60621.5, 242780, 88588, 265161.5, 178274, 133840.5, 2065500), country = c("Austria", "Austria", "Austria", "Austria", "Austria", "Austria", "Austria", "Austria", "Austria"), province = c("Vorarlberg", "Burgenland", "Niederösterreich", "Steiermark", "Kärnten", "Oberösterreich", "Salzburg", "Tirol", "Wien") )
print(cities) smap(cities)
print(cities) smap(cities)
source: [World Cities Database](https://simplemaps.com/data/world-cities)
smap()
also works with file system paths or urls to shapefiles. It even
looks inside zip files if necessary!
smap("https://www.naturalearthdata.com/http//www.naturalearthdata.com/download/50m/cultural/ne_50m_admin_0_countries.zip")
Numeric vectors of length 2 are interpreted as longitude/latitude coordinate pairs. If you supply names, smap uses them to identify the correct order.
smap(c(16.422524, 48.185686)) smap(c(LAT = 48.185686, LoNgItuDE = 16.422524))
smap(c(LAT = 48.185686, LoNgItuDE = 16.422524))
You can call smap()
on existing leaflet objects to add background tiles and
a ruler for measuring distance
library(leaflet) lf <- leaflet::leaflet(height=200) %>% leaflet::addCircleMarkers(lng = 16.422524, lat = 48.185686) lf smap(lf)
library(leaflet) lf <- leaflet::leaflet(height=200) %>% leaflet::addCircleMarkers(lng = 16.422524, lat = 48.185686) leafsync::sync(lf, smap(lf), sync.cursor = FALSE)
Everyone who has worked with simple features will know that it is sometimes a
bit awkward to convert between the different datatypes provided by the package
(sf::sf
, sf::sfc
and sf::sfg
). Toe remedy this smap()
works natively
with all three.
smap(sf::st_point(c(16.373311, 48.208482))) smap(sf::st_sfc(sf::st_point(c(16.373311, 48.208482)))) smap(sf::st_sf(coords = sf::st_sfc(sf::st_point(c(16.373311, 48.208482))), crs = 4326))
smap(sf::st_sf(coords = sf::st_sfc(sf::st_point(c(16.373311, 48.208482))), crs = 4326))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.