如何从`parLapply` 函数调用全局函数? [英] How to call global function from the `parLapply` function?

查看:62
本文介绍了如何从`parLapply` 函数调用全局函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

speedDistribution <- 函数(速度){返回(分位数(速度,seq(0.2,1,by = 0.20)))}估计特征 <- 功能(行程,目标){cl <- makeCluster( 4 )特征 = NULL特征 = parLapply(cl, 1:length(trips), function(z){z <- as.data.frame(z)速度 <- 3.6 * sqrt(diff(z$x)^2 + diff(z$y)^2)s <- 速度[!speed >平均值(速度)+ sd(速度)* 5]特征 = c(speedDistribution(s),target)返回(cbind(特征,rep(z,nrow(特征))))})停止集群(cl)返回(特征)}

<块引用>

checkForRemoteErrors(val) 中的错误:4 个节点产生错误;第一的错误:找不到函数speedDistribution"

解决方案

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

clusterExport(cl, "speedDistribution")

在您尝试计算之前.

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"

解决方案

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.

这篇关于如何从`parLapply` 函数调用全局函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆