Alejandro Gonzalez
Alejandro Gonzalez

Reputation: 67

Can't render loops in powerpoint with Rmarkdown

I have in several chunks loops that produce graphs. I want to make a powerpoint presentation out of them, but the loop doesn't work. It only renders the first graph.

Here is a minimal working example:

---
title: "Title"
author: John Doe
date: 
output: 
  powerpoint_presentation
---
knitr::opts_chunk$set(echo = FALSE, warning=FALSE)
n <- 7
for(i in 1:n){plot(c(1:i))}

I have discovered that the knitr::opts_chunk instructions produces the conflict. If there are no knitr/chunk specifications, it does render the loops. The issue is that I need echo = false, because otherwise it renders many code along with the graphs

Does anyone know how could echo = false (or other chunk options) could "coexist" with rendering loops?

I wouldn't mind rendering it on html but it gaves me other issues as well, similar to this (with tabset)

EDIT: I wasn't clear in the last sentence. If rendered to html, the loops work just fine, but in that case I would need each graph of the loop to be in a different tab, and I have no clue how to do that. Also, powerpoint makes it easier for me for further formatting.

Best regards.

Upvotes: 2

Views: 123

Answers (1)

shirewoman2
shirewoman2

Reputation: 1938

I'm not sure if you're calling a child .Rmd document here, but that's my approach for this sort of thing. For that approach, you would have one coding chunk in the parent .Rmd file that runs the loop. The results option for that chunk must be set to:

 results = "asis"

Then, inside that chunk, that's where I call on my child .Rmd file. One potential trouble spot there is that the child .Rmd file cannot have any named chunks or you'll get errors when you try to knit because of replicate chunk names. Maybe there's a way around that, but I haven't gotten it to work.

Here's an example of what my parent .Rmd file chunk looks like where it runs the loop:

 ```{r myloop, echo = FALSE, results = "asis"} 
 for(i in 1:10){
      IndivSlide <- knitr::knit_child("IndividualSlides.Rmd", 
                                 envir = environment(), 
                                 quiet = TRUE)

     cat(IndivSlide, sep = "\n")
 }
 ``` 

And then the file "IndividualSlides.Rmd" is set up like a regular rmarkdown file except that you don't have that heading at the top with the title, author, etc.

You asked about PowerPoint slides, specifically, but this works the same way for Word files.

Upvotes: 0

Related Questions