Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RMarkdown: bookdown with plotly

I'm using the bookdown package with RMarkdown to generate web-based book similar to this, likewise with the option to download a pdf-version of the book.

I've included plotly graphs in my "book" which work nicely in the html-version of the book. Being interactive, the button "build book" throws an error when including pdf output in the YAML-header.

Based on this description I've found a workaround with a regular RMarkdown File to create pdfs with plotly graphs outputs. A minimal solution (outside bookdown) looks like this:

---
title: "test"
output:
  pdf_document: default
---

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

Sys.setenv("plotly_username" = "username")
Sys.setenv("plotly_api_key" = "API")
```


```{r cars}
library(plotly)
p <- plot_ly(x = c(1,2,3,4), y = c(2,4,1,3), type = 'scatter', mode = 'lines')
plotly_IMAGE(p, format = "png", out_file = "output.png")

```
![Caption for the picture.](output.png)

Is there a way to include this solution within bookdown, so that the graphs are automagically interactive in the html output and static (png) in the pdf output?

like image 550
Ratnanil Avatar asked Oct 16 '17 22:10

Ratnanil


People also ask

Can you use Plotly in Rmarkdown?

If you are creating R charts in an RMarkdown environment with HTML output (such as RStudio), simply printing a graph you created using the plotly R package in a code chunk will result in an interactive HTML graph in the viewer.

Does Plotly work in R?

Plotly is a free and open-source graphing library for R. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

What is knitr Rmarkdown?

R Markdown is a variant of Markdown that has embedded R code chunks, to be used with knitr to make it easy to create reproducible web-based reports. The Markdown syntax has some enhancements (see the R Markdown page); for example, you can include LaTeX equations (see Equations in R Markdown).

How do I show plots in R markdown?

In RStudio, when you open a new RMarkdown file, in the editor pane, there is a cogwheel button / menu where you can choose "Chunk Output Inline". That should put the plots into the document.


1 Answers

Based on this blogpost I was able to come up with a solution. Note, this only works

  • with the "knitr" button (see this post for a workaround)
  • with an internet connection (1) and Plotly Account (2)

(1) See this link to export static images locally (which I didn't get to work since I failed installing PhantomJS)

(2) Plotly has a user Quota of currently 100 plots / grids per user per day

---
title: "test"
output:
  html_document: default
  pdf_document: default
  word_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(plotly)
library(pander)
Sys.setenv("plotly_username" = "YOUR PLOTLY USERNAME")
Sys.setenv("plotly_api_key" = "API KEY")

output <- knitr::opts_knit$get("rmarkdown.pandoc.to") # html / latex / docx

```


```{r, results="asis"}

print(output)

p <- plot_ly(x = c(1,2,3,4), y = c(2,4,1,3), type = 'scatter', mode = 'lines')

filename <- "output.png"

if(output %in% c("latex","docx")){
  plotly_IMAGE(p, format = "png", out_file = filename)
  pandoc.image(filename)
} else if(output == "html"){
  p
} else(print("No format defined for this output filetype"))


```
like image 143
Ratnanil Avatar answered Nov 03 '22 20:11

Ratnanil