0

Depending on user input, I would like to generate a single knitted Word document with multiple copies of a set of coding and text chunks, but I haven't been able to find a place online that does this. For example, say I've got the beginnings of an rmarkdown file like this:

---
title: "Example"
output: word_document
---

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

# The best fruit to buy today

```{r}

# Read in a file that has a bunch of options for running "myfancyFun", so each
# fruit would have its own set of parameters for option1, option2, etc.
InputFruitFile <- read.csv(file = "Fruit options.csv")

for(i in 1:nrow(InputFruitFile)){
    
    myfancyFun(fruit = i, 
               option1 = InputFruitFile$Option1[i], 
               option2 = InputFruitFile$Option2[i], 
               option3 = InputFruitFile$Option3[i])
}

```

Every time there's an iteration i, I'd like for "myfancyFun" to generate a new set of text and coding chunks that would be included in a single Word document. One day, maybe it's mangoes and pineapples that look good, so that Word document would have only two sets of those chunks. Another day, maybe it's mangoes, pineapples, peaches, cherries, and grapes, so that would have 5 sets of coding and text chunks. The coding and text chunks that are output from "myfancyFun" are complicated enough that using something like this with cat and sprintf are not going to cut it. The output from "myfancyFun" has multiple sections with graphs, text describing the graphs, then a table or two, etc., so I want all the options that a typical rmarkdown file offers; I just also want to be able to loop it.

Is this even possible?

shirewoman2
  • 1,842
  • 4
  • 19
  • 31
  • Store the `myfancyFun` in `result` and then print the result for each iteration of the loop inside the loop itself: `cat(result)` – Talha Asif Feb 02 '23 at 15:57

0 Answers0