[英]clusterExport, environment and variable scoping
I wrote a function in which I define variables and load objects. 我写了一个定义变量和加载对象的函数。 Here's a simplified version:
这是一个简化的版本:
fn1 <- function(x) {
load("data.RData") # a vector named "data"
source("myFunctions.R")
library(raster)
library(rgdal)
a <- 1
b <- 2
r1 <- raster(ncol = 10, nrow = 10)
r1 <- init(r1, fun = runif)
r2 <- r1 * 100
names(r1) <- "raster1"
names(r2) <- "raster2"
m <- stack(r1, r2) # basically, a list of two rasters in which it is possible to access a raster by its name, like this: m[["raster1"]]
c <- fn2(m)
}
Function "fn2" is can be found in "myFunctions.R" and is defined as: 函数“ fn2”可在“ myFunctions.R”中找到,并定义为:
fn2 <- function(x) {
fn3 <- function(y) {
x[[y]] * 100 * data
}
cl <- makeSOCKcluster(8)
clusterExport(cl, list("x"), envir = environment())
clusterExport(cl, list("a", "b", "data"))
clusterEvalQ(cl, c(library(raster), library(rgdal), rasterOptions(maxmemory = a, chunksize = b)))
f <- parLapply(cl, names(x), fn3)
stopCluster(cl)
}
Now, when I run fn1, I get an error like this: 现在,当我运行fn1时,出现如下错误:
Error in get(name, envir = envir) : object 'a' not found
From what I understand from ?clusterExport, the default value for envir is .GlobalEnv, so I would assume that "a" and "b" would be accessible to fn2. 根据我对?clusterExport的了解,envir的默认值为.GlobalEnv,因此我假设fn2可以访问“ a”和“ b”。 However, it doesn't seem to be the case.
但是,事实并非如此。 How can I access the environment to which "a" and "b" belong?
如何访问“ a”和“ b”所属的环境?
So far, the only solution I have found is to pass "a" and "b" as arguments to fn2. 到目前为止,我发现的唯一解决方案是将“ a”和“ b”作为参数传递给fn2。 Is there a way to use these two variables in fn2 without passing them as arguments?
有没有一种方法可以在fn2中使用这两个变量而不将其作为参数传递?
Thanks a lot for your help. 非常感谢你的帮助。
You're getting the error when calling clusterExport(cl, list("a", "b", "data"))
because clusterExport
is trying to find the variables in .GlobalEnv
, but fn1
isn't setting them in .GlobalEnv
but in its own local environment. 你打电话时得到错误
clusterExport(cl, list("a", "b", "data"))
因为clusterExport
试图寻找在变量.GlobalEnv
,但fn1
不设置它们.GlobalEnv
但在自己的本地环境中。
An alternative is to pass the local environment of fn1
to fn2
, and specify that environment to clusterExport
. 一种替代方法是将
fn1
的本地环境fn1
给fn2
,并将该环境指定为clusterExport
。 The call to fn2
would be: 对
fn2
的调用为:
c <- fn2(m, environment())
If the arguments to fn2
are function(x, env)
, then the call to clusterExport
would be: 如果
fn2
的参数是function(x, env)
,那么对clusterExport
的调用将是:
clusterExport(cl, list("a", "b", "data"), envir = env)
Since environments are passed by reference, there should be no performance problem doing this. 由于环境是通过引用传递的,因此这样做不会出现性能问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.