Save dygraph to png in R markdown pdf output?

770 Views Asked by At

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?

1

There are 1 best solutions below

1
On

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