使用 Makefile 并行化时,R markdown 文件与图形重叠

标签 r parallel-processing makefile knitr r-markdown

我创建了一个简单的例子来展示我目前遇到的问题。

我有一个名为 example.Rmd 的 R-markdown 文件,其中包含以下代码

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

和一个包含以下内容的 Makefile 文件
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')"

如果我按顺序运行 Makefile 文件
make

没有问题。

如果我并行运行 makefile
make -j 2

knit2html 函数生成的块重叠,两个 html 文件包含相同的图像。

有什么建议吗?我一直在寻找解决方案,但一无所获。

最佳答案

使用 Karl 的想法,我编写了一个可能的解决方案。

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_$@

对初始代码有两个修改。
  • 正如 Karl 所评论的,我已经包含了 opts_knit$set(base.dir="dir_example0?.html") 行,这样就可以在该路径中创建图形文件夹。
  • 我在 Rscript -e 命令中交换了 "和 ' 符号,如评论 here

  • 并行执行
    make -j 2
    

    工作正常。

    关于使用 Makefile 并行化时,R markdown 文件与图形重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23648815/

    相关文章:

    r - 从矩阵创建 bool 矩阵

    c++ - 如何在生成依赖文件中包含 IBM XLC 模板 *.c 文件?

    c++ - 一个简短的 c++ 文件和 makefile : I can make in the shell,,但在 Eclipse 中构建它时出现很多错误

    c++ - 在 Rcpp 中用随机生成的值填充 vector 的一部分

    r - R中同一张图上的频率和累积频率曲线

    r - 时间序列及其可视化

    c# - .Net中的Dictionary在并行读写时是否有可能导致死锁?

    python - 为什么要使用 tf.train.Server 并行执行多个 tf.Session()?

    c - 在 C 语言中使用 MPI_Reduce

    java - 制造情况!部分程序使用 3 种编程语言。使用哪个构建工具?