I want to include my highchart plots and leaflet map in a pdf file (or docx).
Maybe there is a way simpler way than this here.
I have a test app running on shiny-server that works just fine. webshot2 creates a png and that png will get included in the tex file and then converted to pdf.:
library(shiny)
library(webshot2)
library(tinytex)
ui <- fluidPage(
titlePanel("Wikipedia Screenshot to PDF"),
sidebarLayout(
sidebarPanel(
downloadButton("downloadPDF", "Download PDF")
),
mainPanel(
)
)
)
server <- function(input, output, session) {
output$downloadPDF <- downloadHandler(
filename = function() {
"WikipediaScreenshot.pdf"
},
content = function(file) {
webshot2::webshot(url = "https://example.com/",
file = "example.png")
tex_file <- "temp.tex"
tex_content <- paste(
"\\documentclass{article}",
"\\usepackage{graphicx}",
"\\begin{document}",
"\\includegraphics[width=\\textwidth]{wikipedia.png}",
"\\end{document}",
sep = "\n"
)
writeLines(tex_content, tex_file)
tinytex::pdflatex(tex_file)
file.copy("temp.pdf", file)
file.remove("temp.pdf")
},
contentType = "application/pdf"
)
}
shinyApp(ui, server)
The main difference in my main app is, that i am using a local html file instead of a url for webshot2::webshot(). those htmls are the capture of the highcharter plot and leaflet map, and get created just fine.
But the png that get created from webshot are just blank. i tried to delay the capture but it makes no difference. all png created will also have the same size.
output$downloadReport <- downloadHandler(
filename = function() {
"ComplexAppReport.pdf"
},
content = function(file) {
# Save Highcharts to HTML
htmlwidgets::saveWidget(value_bar_chart(), "value_bar_chart.html")
htmlwidgets::saveWidget(value_bar_chart2(), "value_bar_chart2.html")
# Save Leaflet maps to HTML
mapshot(leaflet_map1(), url = "leaflet_map1.html")
mapshot(leaflet_map2(), url = "leaflet_map2.html")
print(file.exists("value_bar_chart.html"))
print(file.exists("leaflet_map1.html"))
#HTML to PNG
webshot2::webshot(url="/srv/shiny-server/dashboard/value_bar_chart.html", file = "value_bar_chart.png")
webshot2::webshot(url="/srv/shiny-server/dashboard/value_bar_chart2.html",file = "value_bar_chart2.png")
webshot2::webshot(url="/srv/shiny-server/dashboard/leaflet_map1.html",file = "leaflet_map1.png")
webshot2::webshot(url="/srv/shiny-server/dashboard/leaflet_map2.html", file ="leaflet_map2.png")
tex_file <- "temp.tex"
tex_content <- paste(
"\\documentclass{article}",
"\\usepackage{graphicx}",
"\\begin{document}",
"\\includegraphics[width=0.8\\textwidth]{value_bar_chart.png}",
"\\includegraphics[width=0.8\\textwidth]{value_bar_chart2.png}",
"\\includegraphics[width=0.8\\textwidth]{leaflet_map1.png}",
"\\includegraphics[width=0.8\\textwidth]{leaflet_map2.png}",
"\\end{document}",
sep = "\n"
)
writeLines(tex_content, tex_file)
tinytex::pdflatex(tex_file)
file.copy("temp.pdf", file)
file.remove("temp.pdf")
},
contentType = "application/pdf"
)
Or is there another way that i can get my highchart plots and leaflet map into a pdf or docx?
Thanks so much for the help.
You could include the leaflet map and highcharter plots in a quarto document (RMarkdown should work, too).
Make sure that PhantomJS
is installed (webshot::install_phantomjs()
).
Here is an example file:
---
title: "Leaflet & Highcharter to docx"
format: docx
editor: visual
---
```{r}
library(leaflet)
library(highcharter)
```
```{r}
hchart(
iris,
"scatter",
hcaes(x = Sepal.Length, y = Sepal.Width, group = Species)
)
```
```{r}
m <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
m
```
Alternatively use pagedown::chrome_print()
to turn any HTML file into a pdf:
pagedown::chrome_print("value_bar_chart.html")