如何在 foreach() 循环中插入绘图?

How to insert a plot over foreach() loop?

是否可以在 foreach()% dopar{} 循环中添加情节?

我正在使用下面的代码,但它不会使用 foreach 插入图像。不过,它与 for 循环完美配合。

foreach 循环如何工作?

{rm(list = ls())
  library(dplyr)
  library(imager)
  library(openxlsx)
  }

library(foreach)
library(doParallel)
cl <- makeCluster(4)
registerDoParallel(cl)

#for(i in 1:3){ 
foreach(i = 1:3, .combine = cbind, .packages = c("dplyr","imager","openxlsx"))%dopar% {
     file<-paste0(i,".xlsx")
     wb<-createWorkbook(file)
     addWorksheet(wb, "test", gridLines = T)
     Logo<- imager::load.image("~/YY.PNG")
     print(Logo)
     insertPlot(wb, sheet = "test", width = 2, height =1, fileType = "png", units = "in")
     saveWorkbook(wb, file, overwrite = TRUE)
  }
stopCluster(cl)

感谢@MrFlick,答案如下:

library(foreach)
library(doParallel)
cl <- makeCluster(4)
registerDoParallel(cl)

foreach(i = 1:3, .combine = cbind, .packages = c("dplyr","imager","openxlsx"))%dopar% {
  file<-paste0(i,".xlsx")
  wb<-createWorkbook(file)
  addWorksheet(wb, "test", gridLines = T)
  img <- "~/YY.png"
  insertImage(wb, sheet = "test", img, width = 2, height =1, units = "in",startRow = 1, startCol = 1)
  saveWorkbook(wb, file, overwrite = TRUE)
}
stopCluster(cl)