Search code examples
rparallel-processingmclapply

Parallelization: package "parallel" instead of "mclapply"


I am trying to convert this code to the one that can be executed on Windows:

numCores <- detectCores()
results <- mclapply(seq(1, 500), function(file, fID){
  myData <- fread(file.path(dirPath, fID, paste0(file, ".csv")))
  return(cbind(myData, rep(file, nrow(myData))))
}, mc.cores = numCores, fID = 1)

Based on using this tutorial, I wrote the following code...

UPDATE: The correct code is provided below:

getAllMyData <- function(numCores,folderID)
{
  dirPath = paste0("D:/home/", folderID, '/')
  cl <- makeCluster( 4 )
  allTrips = parLapply(cl, 1:200, function(z){
    myData <- read.csv(paste0(dirPath, z, ".csv"))
    return(cbind(myData , rep(z, nrow(myData))))
  })
  stopCluster(cl)  
  return(allTrips)
}

numCores <- detectCores()
allMyData <- getAllMyData(numCores,1)

Solution

  • Your first code calls a function

    function(file, fID)
    

    Your second code, by contrast, uses

    function(dirPath,fID)
    

    That’s the error.