Do flyer flyers in R?

I understand that this pretty much violates the purpose of using the interactive leaflet map, but I am writing a traditional paper book and I want to show how the leaflet package for R. works. I am writing a book in LaTeX and rendering with knitr . Is there a way to display the leaflet map as a bitmap so that it can be included in this book?

Here is a minimal example:

 library(leaflet) map <- leaflet() %>% addTiles() %>% addMarkers(lng = -77.03673, lat = 38.89761) 

Now, if I try a piece like:

 <<>>= map @ 

I get this error:

 Error in validateCssUnit(sizeInfo$width): "\maxwidth" is not a valid CSS unit (eg, "100%", "400px", "auto") 

Trying to save as PNG doesn't work either:

 <<>>= png(filename = "test.png") map dev.off() @ 

map not inherited from ggplot , so ggsave will not work either.

Is there any way to make this work?

+5
source share
1 answer

Recently, a question arose about How to save a leaflet in RStudio as a png or jpg file? . If you don't mind installing PhantomJS , the code below will help you create static images from a flyer (or mapview ). It remains only not to show the saveWidget and webshot in your book, but instead import and display the png file created from it.

 ## install 'webshot' package library(devtools) install_github("wch/webshot") ## load packages library(leaflet) library(htmlwidgets) library(webshot) ## create map m <- leaflet() %>% addTiles() %>% addMarkers(lng = -77.03673, lat = 38.89761) ## save html to png saveWidget(m, "leaflet_map.html", selfcontained = FALSE) webshot("leaflet_map.html", file = "leaflet_map.png", cliprect = "viewport") ## optionally display image when using knitr # p <- knitr::include_graphics("leaflet_map.png") 

And here are some LaTeX output for demo purposes. If anyone is interested, the full .Rnw source file is available from GitHub .

LaTeX

+7
source

Source: https://habr.com/ru/post/1243473/


All Articles