I want create leaflet map with some markers. This works well with HTML output, but I also want to send a pdf to some participants who like to have a hard copy.
It works well for html output, but I get a grey canvas around the map in pdf mode. I tried to change width
and fig.width
and so on, with no real success.
Example file test.Rmd
---
output:
pdf_document: default
html_document: default
---
## Test RMarkup file
```{r echo=FALSE}
library(leaflet)
leaflet() %>%
addTiles %>%
setView(lng = 13.7463, lat = 51.0628, zoom = 12) %>%
addMarkers(lng = 13.7444, lat = 51.06162,
options = markerOptions(title = "Terence Hill Ice Saloon"))
```
PDF output with grey canvas
CodePudding user response:
You could use webshot2
to take a a PDF screenshot of the the html map.
---
output:
pdf_document: default
html_document: default
---
## Test RMarkup file
```{r echo=FALSE}
## load packages
library(leaflet)
library(htmlwidgets)
library(webshot2)
m <- leaflet() %>%
addTiles %>%
setView(lng = 13.7463, lat = 51.0628, zoom = 12) %>%
addMarkers(lng = 13.7444, lat = 51.06162,
options = markerOptions(title = "Terence Hill Ice Saloon"))
## saves html then screenshots to PDF
htmlwidgets::saveWidget(m, "temp.html", selfcontained = FALSE)
webshot("temp.html", file = "m.pdf",
cliprect = "viewport")
```
You could also use webshot2
to export a PNG file which you then can reference in the .Rmd file.
---
output:
pdf_document: default
html_document: default
---
## Test RMarkup file
```{r echo=FALSE, }
## load packages
library(leaflet)
library(htmlwidgets)
library(webshot2)
m <- leaflet() %>%
addTiles %>%
setView(lng = 13.7463, lat = 51.0628, zoom = 12) %>%
addMarkers(lng = 13.7444, lat = 51.06162,
options = markerOptions(title = "Terence Hill Ice Saloon"))
## save html to png
htmlwidgets::saveWidget(m, "temp.html", selfcontained = FALSE)
webshot("temp.html", file = "m.png",
cliprect = "viewport")
```
![Terence Hill Ice Saloon, of course](m.png)
CodePudding user response:
You can do it simplier:
RMD:
---
title: "PIC"
output:
pdf_document
---
Irgendwo spielte Musik.
\begin{figure}
\centering
```{r, echo = F, warning = F, out.width = '100%'}
library(leaflet)
leaflet() %>%
addTiles %>%
setView(lng = 13.7463, lat = 51.0628, zoom = 12) %>%
addMarkers(lng = 13.7444, lat = 51.06162,
options = markerOptions(title = "Terence Hill Ice Saloon"))
```
\end{figure}
Output: