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

限于喜欢 提交于 2021-01-05 06:35:58

问题


How to call global function speedDistribution from the parLapply function?

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"


回答1:


You need to export the function to be available to all workers using the clusterExport function. Add:

clusterExport(cl, "speedDistribution")

Before you attempt your calculations.



来源:https://stackoverflow.com/questions/27907538/how-to-call-global-function-from-the-parlapply-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!