knitr::opts_chunk$set(fig.width = 8.83, mesage = FALSE)
mapview provides a few special popup rendering functions. These can be used to generate:
popupTable
is the standard popup function used in mapview. It provides a static table rendering of all attributes of the individual features.
library(mapview) library(leaflet) library(leafpop) mapview(breweries, popup = popupTable(breweries, zcol = c("brewery", "village", "founded")))
This can also be used with leaflet maps
leaflet() %>% addTiles() %>% addCircleMarkers(data = breweries, popup = popupTable(breweries))
popupGraph
allows us to include static or interactive (htmlwidgets) plots in popups. In the following example we will show a scatterplot of copper ~ cadmium for each individual feature of the famous meuse data set
library(lattice) library(sp) data(meuse) coordinates(meuse) <- ~x+y proj4string(meuse) <- CRS("+init=epsg:28992") p <- xyplot(copper ~ cadmium, data = meuse@data, col = "grey", pch = 20, cex = 2) p <- mget(rep("p", length(meuse))) clr <- rep("grey", length(meuse)) p <- lapply(1:length(p), function(i) { clr[i] <- "red" update(p[[i]], col = clr) }) mapview(meuse, zcol = "cadmium", popup = popupGraph(p))
Here's a rather silly example of an interactive popup. I am sure there's many more sensible examples but for the fun of it here's a map in a map popup.
A WARNING: Including interactive popups can quickly lead to very large files that will take a long time to render and also become rather unresponsive.
brew1 <- breweries91[1, ] mapview(brew1, popup = popupGraph(mapview(brew1, map.types = "Esri.WorldImagery")@map, type = "html"))
popupImage
can be used to include images in popups. Both local (file) and remote (web url) images can be used.
library(sf) pnt <- data.frame(x = 174.764474, y = -36.877245) pnt <- st_as_sf(pnt, coords = c("x", "y"), crs = 4326) img <- "https://upload.wikimedia.org/wikipedia/commons/thumb/6/62/Mount_Eden.jpg/640px-Mount_Eden.jpg" mapview(pnt, map.types = "Esri.WorldImagery", popup = popupImage(img, src = "remote"))
IMPORTANT NOTICE for those who want to save maps locally (see chapter mapview save maps
for details): With the current implementation of popupImage
(local only) and popupGraph
we need to save the popup files (graphs, images) in a folder called popup_graphs
which needs to be located one level above the map widget. Hence, when saving a map locally (with mapshot
or saveWisget
) we need to manually copy that folder to on elevel above the destination folder where the map is saved. This should soon be solved but for the time being, in case you don't see any popup images or graphs in your locally saved map, make sure to copy the appropriate folder (which is usually generated in your computers temp
directory).
There's also a hidden function that enables us to include all sorts of things via Iframe (which is used internally when rendering htmlwidgets graphs).
But let the Dude show us how this can be used...
mapview(pnt, popup = leafpop:::popupIframe("https://www.youtube.com/embed/iApz08Bh53w?autoplay=1", width = 300, height = 225))
As with htmlwidgets in popups, this may quickly produce html files and hence become rather unresponsive.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.