| save_map | R Documentation |
Renders a mapgl map widget to a static PNG file using headless Chrome
via the chromote package. Uses the same html2canvas-based screenshot
infrastructure as add_screenshot_control().
save_map(
map,
filename = "map.png",
width = 900,
height = 500,
include_legend = TRUE,
hide_controls = TRUE,
include_scale_bar = TRUE,
basemap_color = NULL,
image_scale = 1,
background = "white",
delay = NULL
)
map |
A map object created by |
filename |
Character string. The output file path. Defaults to
|
width |
Integer. The width of the map viewport in pixels. Always
overrides any |
height |
Integer. The height of the map viewport in pixels. Always
overrides any |
include_legend |
Logical. Include the legend in the output? Default
|
hide_controls |
Logical. Hide navigation and other interactive controls?
Default |
include_scale_bar |
Logical. Include the scale bar? Default |
basemap_color |
Character string or |
image_scale |
Numeric. Scale factor for the output image. Use |
background |
Character string or |
delay |
Numeric or |
This function requires the chromote and httpuv packages.
Install them with install.packages(c("chromote", "httpuv")). chromote
also requires a Chrome or Chromium browser installation.
The function works by:
Saving the map widget to a temporary HTML file
Opening it in headless Chrome
Waiting for all map tiles and styles to load
Using html2canvas to capture the rendered map (including legends, attribution, and optionally the scale bar)
Decoding the captured image and writing it to the output file
The output file path, invisibly.
## Not run:
library(mapgl)
map <- maplibre(
center = c(-96, 37.8),
zoom = 3
)
save_map(map, "us_map.png")
save_map(map, "us_map_retina.png", image_scale = 2)
# Remove basemap, keep only data layers on white
save_map(map, "data_only.png", basemap_color = "white")
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.