RyanBower
RyanBower

Reputation: 107

Dynamic Slide Generation for RMarkdown / ioslides

One of the things I love about RMarkdown / ioslides is how easy it is to dynamically generate content. Is it possible to take this to the next level and dynamically generate slides?

For example, let's say we wanted to create a deck using the mtcars dataset. Would it be possible to create a deck that generated--dynamically--slides plotting horsepower (hp) and weight (wt), with a slide for each number of cylinders? Let's assume that we want to allow for any count of cylinders and dynamically create a slide for each possible number.

Obviously, this is a simplified example, but for creating appendices on RMarkdown documents, this would be extremely helpful. Should I create an external script? What other methods could I use?

Upvotes: 1

Views: 1338

Answers (1)

tmpname12345
tmpname12345

Reputation: 2921

Sure, you can generate the Markdown syntax using R code to start a new slide and add content. The trick is to use results = "asis" in the chunk options. See below for a minimal example.


title: "Generate slides in R"
output: 
    ioslides_presentation
---


```{r, echo = FALSE, results = "asis"}
for(i in unique(mtcars$cyl)){

  cat("\n\n## Cyl = ", i, "\n\n")
  cat("Here is a plot: \n\n")
  plot(hp ~ wt, data = subset(mtcars, cyl == i))

}

```

Upvotes: 9

Related Questions