如何从 parLapply 函数调用全局函数?

How to call global function from the `parLapply` function?

如何从 parLapply 函数调用全局函数 speedDistribution

speedDistribution <- function(speed)
{
  return(quantile(speed, seq(0.2, 1, by = 0.20)))
}

estimateFeatures <- function(trips,target)
{
  cl <- makeCluster( 4 )
  features = NULL
  features = parLapply(cl, 1:length(trips), function(z){    
    z <- as.data.frame(z)
    speed <- 3.6 * sqrt(diff(z$x)^2 + diff(z$y)^2)
    s <- speed[!speed > mean(speed) + sd(speed) * 5]     
    features = c(speedDistribution(s),target)
    return(cbind(features, rep(z, nrow(features))))
  })
  stopCluster(cl)  
  return(features)
}

Error in checkForRemoteErrors(val) : 4 nodes produced errors; first error: could not find function "speedDistribution"

您需要导出函数以供使用 clusterExport 函数的所有工作人员使用。添加:

clusterExport(cl, "speedDistribution")

在您尝试计算之前。