TiFr3D TiFr3D - 1 month ago 10x
R Question

RmarkDown iterative loop

I have an RMarkdown file that produce a report for a business unit "Ux". I want to make an iterative loop to produce a specific report for all the business unit "U1, U2, U3,..." of the business.

in the Rmarkdown file I have this input :

```{r , warning=FALSE, echo=FALSE, message=FALSE, autodep=TRUE,cache.vars='pole' }
pole <- "21"

where pole is the business unit identification variable.

then i have a R script call that do some stuff with pole variable.

```{r LOAD, warning=FALSE, echo=FALSE, message=FALSE, autodep=TRUE, cache=FALSE }
source('POLE2.r', local=environment())

How can i produce every single report (an html file for each unit) for pole <- [1,2,3,4,5...] with an iterative loop ?


Max Max

You can do this with parametrized reports:

Add the following to the YAML-preamble:

  pole: 21 # default value

In your code change pole <- "21" to pole <- params$pole

Then run a loop like:

poles <- 1:5 # define your set
for (pole in poles)
  rmarkdown::render(input = "yourFile.Rmd", 
      output_file = paste0("yourOutput_", pole, ".html"),
      params = list(pole = pole)

See http://rmarkdown.rstudio.com/developer_parameterized_reports.html for more details.