0

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?

Dar L.
  • 49
  • 6
  • https://stackoverflow.com/questions/43396667/conditional-slides-in-r-markdown-beamer-presentation If you're working with LaTeX, you could use an ifelse logic. https://tex.stackexchange.com/questions/108784/else-if-in-algoritmic-package – Roman Luštrik Jun 04 '17 at 08:49
  • Thanks. I'm still hoping to find a solution with R Markdown – Dar L. Jun 04 '17 at 09:32

2 Answers2

1
---
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")

}
```
Alex
  • 4,925
  • 2
  • 32
  • 48
1

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")
```
tchevrier
  • 1,041
  • 7
  • 16