Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R markdown files overlap figures when parallelized using Makefile

I've created a simple example showing the problem I currently have.

I have a R-markdown file, named example.Rmd, containing the following code

```{r}
plot(rnorm(10000))
```

and a Makefile file with the following content

all : example01.html example02.html

example01.html : example.Rmd
    Rscript -e "library(knitr); knit2html(input='example.Rmd', output='example01.html')"

example02.html : example.Rmd
    Rscript -e "library(knitr); knit2html(input='example.Rmd', output='example02.html')"

If I run the Makefile file sequentially

make

there is no problem.

If I run the makefile in parallel

make -j 2

the chunks generated by knit2html function overlap and both html files contains the same image.

Any suggestion? I've been searching for a solution but I've found nothing.

like image 708
marc1s Avatar asked May 14 '14 07:05

marc1s


1 Answers

Using the idea of Karl, I've written a possible solution.

all : example01.html example02.html

example01.html : example.Rmd
    mkdir -p dir_$@ 
    Rscript -e 'library(knitr); opts_knit$$set(base.dir = "dir_$@"); knit2html(input="example.Rmd", output="dir_$@/$@")'
    mv dir_$@/$@ .
    rm -r dir_$@

example02.html : example.Rmd
    mkdir -p dir_$@
    Rscript -e 'library(knitr); opts_knit$$set(base.dir = "dir_$@"); knit2html(input="example.Rmd", output="dir_$@/$@")'
    mv dir_$@/$@ .
    rm -r dir_$@

There are two modifications respect to initial code.

  1. As Karl commented, I've included the line opts_knit$set(base.dir= "dir_example0?.html") in such a way the figure folder is create in that path.
  2. I've swap " and ' symbol in Rscript -e command as commented here

Parallel execution

make -j 2

works fine.

like image 186
marc1s Avatar answered Oct 02 '22 03:10

marc1s