10

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.

Stanislav
  • 551
  • 7
  • 21
  • 7
    See [this](https://gist.github.com/hrbrmstr/17bc21af55392f23f012f57bb2fda51c) gist by [hrbrmstr](https://stackoverflow.com/users/1457051/hrbrmstr) – Kevin Arseneau Dec 02 '17 at 03:16

2 Answers2

3

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

0

Specify different output_files. Then the cache and temp dirs get named after the output file.

dsz
  • 4,542
  • 39
  • 35