Loop for output files

Hello,
I don't know how to perform a loop in order to produce many reports (html).

I assign the same code in my Rmarkdown, but I need to specify a value in order to filter data and produce the html files. It's a simple but annoying to edit 10 times the rx. I was thinking in something as:

for (rx 1:10)  {

Rmakdown

outputname_rx

}

But I don't know how to put the Rmarkdown code inside a for statement.
Thanks for your time and interest

1 Like

I think you would be interesting by parametrized report.

See

I let you try and give me feeback here if it works for you. Thanks !

I think I solved It. I don't know if It's the right approach.


library(knitr)
library(markdown)
library(rmarkdown)


zone_list=c(4,5,13,8,9)


#rm(list=setdiff(ls(),c("rx","listar")))  por  rm(list=(ls()))
# en línea 17 del Reporte.Rmd (regional)


rm(list=setdiff(ls(),c("rx","zone_list")))

for (i in 1:NROW(zone_list=)){

rx=listar[i]
rmarkdown::render('/home/juan/Documents/recolectores/Reporte.Rmd',  
                    output_file =  paste("Reporte_",zone_list=i], ".html", sep=''))
  }

I run this and works fine. But really, maybe It's a bit dirty.
Thanks for your replies.

This is a good approach the for loop !

Is rx an object you use inside your Rmd document ?
If so, currently it works because by default render has access to the value of the parent frame. Another approach would be to use it as a param in the Rmd file.

You yaml header would contain

params:  
    rx:  <default value>

and use params$rx in place of rx in your doc.
and it you call you would do

for (i in 1:NROW(zone_list=)) {
rmarkdown::render('/home/juan/Documents/recolectores/Reporte.Rmd',  
                    output_file =  paste0("Reporte_", zone_list[i], ".html"),
                    param = list(rx = listar[i]))
  }

Parametrized report are really powerful has they allow you to get different type of outputs based on one source file. If you use product like RStudio Connect, this would be the way to go.

Anyway, your solution is completly fine ! Well done !

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.