Ahm MMM
Ahm MMM

Reputation: 119

How to run a rmarkdown to create multiple html with different parameters?

I have a code in R, which is done in rMarkdown, it needs to run several times but with different parameter values.

How can I solve it? I have put an example of simplified problems.

Suppose I need to print different values in a loops, in order to make it simple I used simple loops.

For each combination of these two loops I want to make one html file, for example means 200 *400 different html report files.

---
title: "file1"
author: "user"
date: "25 1 2021"
output: html_document
---

# The first loop should be done, from 1:100, 101:200 ... to ...   20001:20100
  ```{r}
  i=getfirst()
  j=getsecond()
  ```

```{r}

for (i in i:j) print(i)
```

# The second loop should be done, from 1:50, 51:100  ... to ...  20001:20050
```{r}
for (i in i-50:j-50) print(i)
```

suppose each time we may have different i and j which should pass into markdown file.

Upvotes: 0

Views: 1121

Answers (2)

Moritz Schwarz
Moritz Schwarz

Reputation: 2519

I'd simply create a wrapper script (i.e. a separate e.g. .R) file, where you specify:

R file

for (i in 1:10){
  j = i+50
  if(!dir.exists(paste0("directory_",i))){dir.create(paste0("directory_",i))}
  knitr::knit(input = "markdown.Rmd",output = paste0("directory_",i,"/output",i,"_",j,".html"))
}

And then your Rmd file, which you have saved as markdown.Rmd in this example looks like this:

RMarkdown file

---
title: "file1"
author: "user"
date: "25 1 2021"
output: html_document
---

# The first loop should be done, from 1:100, 101:200 ... to ... 20001:20100

```{r}
for (i in i:j) print(i)
```

# The second loop should be done, from 1:50, 51:100 ... to ... 20001:20050

```{r}
for (i in i-50:j-50) print(i)
```

Upvotes: 1

giocomai
giocomai

Reputation: 3528

There is a dedicated chapter in the official documentation, "Knitting with parameters", that outlines how to proceed in your use-case.

Upvotes: 1

Related Questions