Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rmarkdown::render in parallel

I have the R package, one of its function - produce report. In the inst/markdown I have a template rep.rmd. In the package function ProduceReport() I have this code:

  render.file <-"rep.Rmd"
  render.file <- system.file(TEMPLATES.PATH, render.file, package=getPackageName())
  render.dir <- dirname(render.file)
  pdf.file <- "example.pdf"
  rmarkdown::render(render.file , quiet = FALSE, output_format = "pdf_document")

It works, but during the execution markdown produces directories temp files

rep_cache, rep_files

I would like to test this report generating function in parallel (when .rmd files run with different inputs and produce different reports). My first question, is it possible to run same .rmd file with different inputs in parallel?

I guess that temp directories should have unique names to avoid writing to the same files. I found arguments

intermediates_dir = , knit_root_dir =

in the rmarkdown::render() function. But when I try to define this parameters with created dir, pandoc produces errors (and rep_cache, rep_files directories are still in their places).

Please, any advice.

like image 573
Stanislav Avatar asked May 03 '26 10:05

Stanislav


1 Answers

As @Kevin_Arseneau pointed, see:

library(doParallel)

rpts <- list(list(out="one.html", params=list(some_var="One")),
             list(out="two.html", params=list(some_var="Two")),
             list(out="three.html", params=list(some_var="Three")),
             list(out="four.html", params=list(some_var="Four")))

do_rpt <- function(r) {

  require(rmarkdown)

  tf <- tempfile()
  dir.create(tf)

  rmarkdown::render(input="tstrpt.Rmd",
                    output_file=r$out,
                    intermediates_dir=tf,
                    params=r$params,
                    quiet=TRUE)
  unlink(tf)
  
}

registerDoParallel(cores=3)

foreach(r=rpts, .combine=c) %dopar% do_rpt(r)

Sample file:

---
title: Test Report
output: html_document
params:
  some_var: "default"
  some_date: !r as.Date("2015-01-01")
---

Report for `r params$some_var` run on `r params$some_date`

Source: hrbrmstr

like image 159
Yuri Gelsleichter Avatar answered May 04 '26 23:05

Yuri Gelsleichter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!