0

I use R markdown with dygraph(), when i am working i want the output to be an html document, allowing me to enjoy dygraph() features. But at the end of my work i want the ouput to be in a pdf. So all dygraphs need to be converted to .png.

---
title: "DygraphtoPng"
author: "Tibo"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Including Plots


```{r pressure, echo=FALSE}
require(dygraphs)
require(knitr)
require(webshot)
require(htmlwidgets)


if (`r output` == "html_document") {
  dygraph(pressure)
} else {
  dy <- dygraph(pressure)

}  

```

I know webshot()package can help doing it but i don't understand how?

Tibo
  • 68
  • 1
  • 7
  • Maybe an idea, open the html document in Chrome and print as pdf! – ricoderks Dec 11 '19 at 12:30
  • Thank you for your comment. HTML rendering is very different from latex rendering (page format, typo, theme,...). Moreover, HTML output is on a single very long page, when I print it figures are cut in half, margins are poorly set up and so on.. – Tibo Dec 11 '19 at 14:33

1 Answers1

0

You could modify the extension based on your requirements as .pdf, .png, ...

The code:

---
  title: "DygraphtoPng"
author: "Tibo"
output: html_document
---

  ```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Including Plots


```{r pressure, echo=FALSE}
require(dygraphs)
require(knitr)
require(webshot)
require(htmlwidgets)



dy <- dygraph(pressure)

htmlwidgets::saveWidget(widget = dy, file = "dy.html")
webshot(url = "dy.html", file = "dy.pdf", delay = 1, zoom = 4, vheight = 500)

The .png output enter image description here

ozturkib
  • 1,493
  • 16
  • 28
  • I have prepared a blog post including various details about webshot2. You can see the details from http://www.ozturkibrahim.com/export-save-r-graphs-with-webshot/ – ozturkib Apr 28 '20 at 15:30