R: Possible to only run a plotly chunk if knitting to HTML and only a ggplot2 chunk if knitting to PDF or DOCX?

Viewed 460

Current situation: I'm knitting an rMarkdown document, and it's working exactly as expected. When knitting to HTML, each of 10 plotly chunks generate an interactive plotly chart and they work wonderfully. When knitting to PDF or DOCX, each code chunk is shown but each plot does not (as expected), since those file types can't display HTML content.

Goal: Get the plotly plots to render when knitting to HTML, but have ggplots render in their places when knitting to PDF or DOCX (and not echo, eval, or render at all under HTML), without manually adjusting the echo and/or eval parameters for each plot on each knit run.

Tried:

  • Can create a param in the YAML header, but couldn't make it adaptable to the output element of the YAML header.
  • Couldn't directly access the output element of the YAML header from within a chunk to set an if-else conditional.

Current non-preferred options:

  1. Leave the situation as is, with code showing up in all three outputs but plots only showing up in the HTML output (incomplete report for PDF and DOCX).

  2. Code an additional ggplot2 chunk for each plotly chunk and set all echo and eval to true, resulting in one interactive plot and one static plot for each of 10 variations in the HTML output (20 plots total, very repetitive), as well as the PDF and DOCX outputs echoing and evaluating both code chunks (repetitive) but rendering only the ggplots (large amounts of out-of-place unrendered plotly code).

  3. Code an additional ggplot2 chunk for each plotly chunk, and manually adjust the echo and eval parameters for each knit run (easy to miss something and make a mistake).

Needed: An elegant solution to automatically echo and eval code chunk A along with ignoring code chunk B when knitting HTML, while automatically echoing and evaluating code chunk B along with ignoring code chunk A when knitting PDF or DOCX.

1 Answers

The knitr packages contains two functions you can use to solve your problem: knitr::is_html_output() and knitr::is_latex_output(). If you evaluate these functions in the code chunk header you can use plotly when the output is html and ggplot when the output is pdf, e.g.

---
title: "test"
author: "Jared_Mamrot"
date: "05/05/2021"
output:
  pdf_document: default
  html_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
#install.packages("plotly")
library(plotly)
library(tidyverse)
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
summary(cars)
```

## HTML only section

```{r eval=knitr::is_html_output(), echo=FALSE}
test_plot <- ggplot(cars, aes(x = speed, y = dist)) +
  geom_point()
ggplotly(p = test_plot)
```

## PDF only section

```{r eval=knitr::is_latex_output(), echo=FALSE}
test_plot <- ggplot(cars, aes(x = speed, y = dist)) +
  geom_point()
test_plot
```
Related