R Leaflet
- Leaflet is a titanically popular, lightweight JavaScript package designed to simplify web mapping, developed by Vladamir Agafonkin and collaborators since 2011. It’s so popular that users of other languages are beginning to write wrappers that allow you to send data from data science and statistical computing environments (R, Python) to Leaflet maps with a minimum amount of pain.
- One of these wrappers is Leaflet for R, developed by Joe Cheng of RStudio. Let’s take some data from our CARTO database and make a web map in VERY FEW lines of code.
library("sp")
library("rCartoAPI")
library("leaflet")
library("geojsonio")
library("rcartoapi")
sql_inquiry_save_geojson("SELECT * FROM g_hh_results", "~/Desktop/CARTO-RLeaflet/g_hh_results.geojson")
vmt_results <- geojsonio::geojson_read("~/Desktop/CARTO-RLeaflet/g_hh_results.geojson", what = "sp")
pal <- colorNumeric("viridis", NULL)
leaflet(vmt_results) %>%
addTiles() %>%
addPolygons(stroke = FALSE, smoothFactor = 0.3, fillOpacity = 0.8, fillColor = ~pal(right_fit_hh)) %>%
addLegend(pal = pal, values = ~right_fit_hh, opacity = 0.8)