跑步' xlsx'并行处理,使用∥' R包

时间:2017-05-25 10:36:34

标签: r parallel-processing xlsx

我有一个项目,我需要用R处理Excel文件中的一些数据。我必须使用' xlsx'由于某些特定功能而打包。

首先,我写了一个脚本,它可以正常工作而没有错误。

options(java.parameters = "-Xmx4096m") #for extra memory
library(xlsx)

wb <- loadWorkbook(file = "my_excel.xlsx")
sheet1 <- getSheets(wb)[[1]]
rows <- getRows(sheet1)

make_df <- function (x) {
  cells <- getCells(rows[x])
  styles <- sapply(cells, getCellStyle)

  cellColor <- function(style) {
    fg  <- style$getFillForegroundXSSFColor()
    rgb <- tryCatch(fg$getRgb(), error = function(e) NULL)
    rgb <- paste(rgb, collapse = "")
    return(rgb)
  }

  colors <- sapply(styles, cellColor)

  if (!any(colors == "ff0000")) {
    df[nrow(df) + 1, ] <- sapply(cells, getCellValue) #I define this 'df' somewhere in the code; this part could be improved 
  }
}

df <- sapply(1 : length(rows), make_df)

简而言之,我正在寻找没有红色单元格的Excel中的行like described here。问题是Excel文件非常大,需要花费大量时间来处理。

我想要做的是并行运行行检查,以提高效率,所以我补充道:

cl = makeCluster(detectCores() - 1)
clusterEvalQ(cl=cl, c(library(xlsx))) #sharing the package with the workers
clusterExport(cl = cl, c('rows')) #sharing the 'row' variable with the workers

df <- parSapply(cl, 1 : length(rows), make_df)

运行此操作后,我收到以下错误:

Error in checkForRemoteErrors(val) : 
7 nodes produced errors; first error: RcallMethod: attempt to call a method of a NULL object.

我尝试了另一个例子的并行化,没有使用&#39; xlsx&#39;功能,它工作。 经过一番挖掘,我发现this post提供了一些答案(更像是一种解决方法),但我似乎无法实现它。

有没有一种干净的方式去做我想在这里做的事情?

如果没有,那么在这种情况下最好的解决方案是什么?

0 个答案:

没有答案
相关问题