I am trying to use doParallel with RMarkdown. The R code calls a script called report.RMD. The goal is to produce 3 html reports from the iris dataset, each one named after the species with a table in that is filtered down to that species. The problem is that although the reports are rendered, the table doesn't get filtered to that species for example, the virginica.html document has the versicolor species listed. This appears to be a parallelisation issue because when %dopar% is changed to %do%, the html reports are produced as expected.
The wider objective is to use parallel processing with RMarkdown on a larger scale than on this example but am using the below just as an example.
The R code is:
library(doParallel)
library(tidyverse)
iris_list<-c("virginica","versicolor", "setosa")
no_cores <- detectCores() - 1
cl <- makeCluster(no_cores)
registerDoParallel(cl)
foreach(i = 1:length(iris_list), .packages = (.packages())) %dopar% {
cat<-iris_list[i]
iris2<-iris%>%filter(Species==cat)
rmarkdown::render("report.RMD",
output_file = paste0(cat, ".html"))
}
stopCluster(cl)
The RMD code is:
---
title: "Untitled"
date: "28 September 2021"
output: html_document
---
```{r table, echo=FALSE}
iris2