Conditional Chunk Execution in R Markdown

When generating documents with R Markdown, it may be useful to conditionally evaluate certain chunks programmatically.

One way to do so is to use a combination of two knitr and rmarkdown features:

  • Parameters1
  • The “eval” chunk option2

The rmarkdown::render() function accepts a list of parameters to pass into the document while rendering. When written with consideration, these parameters can toggle conditions to be passed to the “eval” chunk option.

The example below is a minimal demonstration of the workflow.

Example

First, we need to write the R Markdown document. In this case, the rendered output will be a PDF conditionally created with the following scenarios:

  • Both a plot and a table
  • Just a plot
  • Just a table
  • Neither a plot nor a table

The parameters passed in will be logicals for “plot” and “table”. These values are used to establish conditions in a chunk near the top of the R Markdown file.

The full .Rmd file:

 --- 
 title: "Sleep Summary" 
 output: pdf_document 
 params: 
   plot: TRUE 
   table: TRUE 
 --- 
  
 ```{r setup, include=FALSE} 
 knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE) 
 library(tidyverse) 
  
 ## establish conditions 
 eval_plot <- params$plot 
 eval_table <- params$table 
 eval_both <- eval_plot & eval_table 
 eval_one_of <- eval_plot | eval_table 
 eval_neither <- !eval_one_of 
 ``` 
  
 ```{r, prep, eval=eval_one_of} 
 knitr::asis_output("## Summary") 
  
 ## summarize msleep data 
 msleep_summary <- 
   msleep %>% 
   group_by(order) %>% 
   summarise(sleep = round(mean(sleep_total),2), .groups = "drop") %>% 
   arrange(desc(sleep)) 
 ``` 
  
 ```{r, table-heading, eval=eval_table} 
 knitr::asis_output("### Table") 
 ``` 
   
 ```{r, table, eval=eval_table} 
 knitr::kable(msleep_summary) 
 ``` 
  
 ```{r, plot-heading, eval=eval_plot} 
 knitr::asis_output("### Plot") 
 ``` 
  
 ```{r, plot, eval=eval_plot} 
 msleep_summary %>% 
   ggplot(aes(reorder(order,sleep), sleep)) + 
   geom_col() + 
   coord_flip() + 
   labs(x = NULL, y = "Sleep total (Hours)") + 
   theme_minimal() 
 ``` 
  
 ```{r, empty-msg, eval=eval_neither} 
 knitr::asis_output("## WARNING: Neither plot nor table generated ...") 
 ``` 
  

Assuming the content above is stored in a file called example.Rmd we can trigger the processing for each condition.

To generate a PDF with both the plot and the table:

rmarkdown::render("example.Rmd", 
                  output_file = "example-both.pdf", 
                  params = list(plot = TRUE, table = TRUE))

This browser does not support PDFs. Download the PDF to view it: Download PDF.

To generate a PDF with just the plot:

rmarkdown::render("example.Rmd", 
                  output_file = "example-plot.pdf", 
                  params = list(plot = TRUE, table = FALSE))

This browser does not support PDFs. Download the PDF to view it: Download PDF.

To generate a PDF with just the table:

rmarkdown::render("example.Rmd", 
                  output_file = "example-table.pdf", 
                  params = list(plot = FALSE, table = TRUE))

This browser does not support PDFs. Download the PDF to view it: Download PDF.

To generate a PDF with neither:

rmarkdown::render("example.Rmd", 
                  output_file = "example-neither.pdf", 
                  params = list(plot = FALSE, table = TRUE))

This browser does not support PDFs. Download the PDF to view it: Download PDF.

Related