2

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"

Klausos Klausos
  • 15,308
  • 51
  • 135
  • 217
  • Merely guessing here, `parApply()` is creating/hacking some local environment where global scoping is not allowed (by default, R searches upwards through the environments). Try putting the `speedDistribution()` function inside your lambda function for `parLapply()`. – Vlo Jan 12 '15 at 18:34

1 Answers1

3

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.

mlegge
  • 6,763
  • 3
  • 40
  • 67