Dar L.
Dar L.

Reputation: 49

Generating slides in Beamer from R Markdown if condition is met

When creating a beamer presentation from R Markdown (with R studio), I sometimes need to create extra slides that contain additional information.

I’m wondering how do I create a new slide only if a condition is met?

Upvotes: 0

Views: 631

Answers (2)

tchevrier
tchevrier

Reputation: 1171

If I may, Alex's answer can be simplified further with easier control, in particular if you have a long and complex document - you can use a conditional chunk as suggested in Xie Yihui's user guide. Here's a tentative MWE:

---
title: "Untitled"
output: beamer_presentation
params:
  your_condition: false  # or set it to true
---

## R Markdown

Some Text

```{r chunk_name, eval = params$your_condition, echo=FALSE, results='asis'}
##  This slide shows up only if your_condition is true
cat("## Conditional Slide")
cat('\n')  
cat("Your Conditional Slide")
```

Upvotes: 1

Alex
Alex

Reputation: 4995

---
title: "Untitled"
output: beamer_presentation
---

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

## R Markdown

Some Text

```{r, results='asis'}
if(TRUE){

  cat("## Conditional Slide")
  cat('\n')  
  cat("First Conditional Slide")

}
```

```{r, results='asis'}
if(FALSE){

  cat("## Conditional Slide")
  cat('\n')  
  cat("Second Conditional Slide")

}
```

Upvotes: 1

Related Questions