如何在R中表示函数的环境?

时间:2021-05-02 23:27:40

I am getting the following error because I believe a clusterExport() (parallel package) I'm doing is referring to the wrong environment:

我得到以下错误,因为我认为我所做的一个clusterExport()(并行包)指的是错误的环境:

Error in get(name, envir = envir) : object 'simulatedExpReturn' not found

I am getting this in a function and specifically at the clusterExport() line of this part:

我在函数中,特别是在这个部分的clusterExport()行中得到了这个:

  simulatedExpReturn = list()

  # Calculate the number of cores
  no_cores <- detectCores()

  # Initiate cluster
  cl <- makeCluster(no_cores)

  clusterExport(cl, c("simulatedExpReturn",
                  "covariance",
                   "numAssets",
                   "assetNames",
                   "numTimePoints-lag",
                   "stepSize"), envir = environment(Michaud1998MonteCarlo))

covariance, numAssets, assetNames, numTimePoints-lag, and stepSize are all passed into the function. I have also tried envir = envir and envir = .GlobalEnv and neither worked.

协方差、numAssets、assetNames、numTimePoints-lag和stepSize都传递给函数。我也尝试过envir = envir =。globalenv,但都没有成功。

How can this be fixed?

这怎么可能是固定的呢?

2 个解决方案

#1


1  

This is a scoping problem, the clusterExport function is searching for your objects in the specified environment, and exports them to each processor's child environment. It does not search the .GlobalEnv where you have defined simulatedExpReturn.

这是一个范围问题,clusterExport函数正在指定环境中搜索对象,并将它们导出到每个处理器的子环境中。它不搜索定义simulatedExpReturn的. globalenv。

This is why the following returns 1 and not an empty list:

这就是为什么以下返回1而不是空列表:

> Michaud1998MonteCarlo <- new.env()
> simulatedExpReturn = list()
> assign("simulatedExpReturn", 1, envir = Michaud1998MonteCarlo)
> 
> # Calculate the number of cores
> no_cores <- detectCores()
> 
> # Initiate cluster
> cl <- makeCluster(no_cores)
> 
> clusterExport(cl, c("simulatedExpReturn"), envir = Michaud1998MonteCarlo)
> clusterCall(cl, function() simulatedExpReturn)
[[1]]
[1] 1

[[2]]
[1] 1

[[3]]
[1] 1

[[4]]
[1] 1

To resolve, simply assign the value to the environment before running the clusterExport:

要解决这个问题,只需在运行clusterExport之前为环境分配值:

assign("simulatedExpReturn", list(), envir = Michaud1998MonteCarlo)

#2


1  

A simple example of passing variable via its name to another function:

通过变量名传递给另一个函数的简单示例:

print.variable.from.env <- function (x,e) { cat("Echoing", get(x, envir = e)) }

my.f <- function()
{
    my.local <- "my local "
    print.variable.from.env("my.local", environment())

}

my.f()

if you run it, it will simply print

如果您运行它,它将只是打印

Echoing my local 

i.e. by passing the environment to print.variable.from.env, the function is able to get access to the varialbe given by its name in x

例如,通过将环境传递给print.variable.from.env,函数就能够访问x中它的名称所给出的变量

And one more example:

一个例子:

print.variable.from.env <- function (x,e) { cat("Echoing", get(x, envir = e), "\n") }

my.f <- function()
{
    my.local <- "my local "
    print.variable.from.env("my.local", environment())
    print.variable.from.env("global.variable", parent.env(environment()))
}

global.variable <- "global"

my.f()

This shows the access to "global.variable" from function's parent env. When executed it'll print

这显示了对“global”的访问。变量"来自函数的父env。执行时它会打印

Echoing my local  
Echoing global 

#1


1  

This is a scoping problem, the clusterExport function is searching for your objects in the specified environment, and exports them to each processor's child environment. It does not search the .GlobalEnv where you have defined simulatedExpReturn.

这是一个范围问题,clusterExport函数正在指定环境中搜索对象,并将它们导出到每个处理器的子环境中。它不搜索定义simulatedExpReturn的. globalenv。

This is why the following returns 1 and not an empty list:

这就是为什么以下返回1而不是空列表:

> Michaud1998MonteCarlo <- new.env()
> simulatedExpReturn = list()
> assign("simulatedExpReturn", 1, envir = Michaud1998MonteCarlo)
> 
> # Calculate the number of cores
> no_cores <- detectCores()
> 
> # Initiate cluster
> cl <- makeCluster(no_cores)
> 
> clusterExport(cl, c("simulatedExpReturn"), envir = Michaud1998MonteCarlo)
> clusterCall(cl, function() simulatedExpReturn)
[[1]]
[1] 1

[[2]]
[1] 1

[[3]]
[1] 1

[[4]]
[1] 1

To resolve, simply assign the value to the environment before running the clusterExport:

要解决这个问题,只需在运行clusterExport之前为环境分配值:

assign("simulatedExpReturn", list(), envir = Michaud1998MonteCarlo)

#2


1  

A simple example of passing variable via its name to another function:

通过变量名传递给另一个函数的简单示例:

print.variable.from.env <- function (x,e) { cat("Echoing", get(x, envir = e)) }

my.f <- function()
{
    my.local <- "my local "
    print.variable.from.env("my.local", environment())

}

my.f()

if you run it, it will simply print

如果您运行它,它将只是打印

Echoing my local 

i.e. by passing the environment to print.variable.from.env, the function is able to get access to the varialbe given by its name in x

例如,通过将环境传递给print.variable.from.env,函数就能够访问x中它的名称所给出的变量

And one more example:

一个例子:

print.variable.from.env <- function (x,e) { cat("Echoing", get(x, envir = e), "\n") }

my.f <- function()
{
    my.local <- "my local "
    print.variable.from.env("my.local", environment())
    print.variable.from.env("global.variable", parent.env(environment()))
}

global.variable <- "global"

my.f()

This shows the access to "global.variable" from function's parent env. When executed it'll print

这显示了对“global”的访问。变量"来自函数的父env。执行时它会打印

Echoing my local  
Echoing global