0

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.

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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)
```

8 Comments

thanks, how can I pass parameter to markdown file?
When you execute the knit command, it uses your working directory as well - so the run_chunk variable is automatically passed to the markdown file. So this should work and result in two different html files with different output.
please take a look at my new code, suppose each time I may have different parameter, I need to store e.g. 100s different html files.
Ok, now I've modified the R file to just create a simple loop over some i values. The j here is just a standard i + 50 but you can wrap another j loop around it if you like. Does that work now?
The I recommend saving the path to both the csv and the html as a variable and then using that variable when saving. If the csv file is already in that folder, then you can find out the path dynamically using list.files - if you want to save it, then you can copy the paste0 command for a write_csv command or something. I'm afraid it's a bit difficult for me to understand your exact workflow apart from the above...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.