在R中,如何使函数内的变量可用于该函数内的较低级别函数?(with,attach,environment) [英] In R, how to make the variables inside a function available to the lower level function inside this function?(with, attach, environment)
问题描述
更新 2@G.Grothendieck 发布了两种方法.第二个是改变函数内部的函数环境.这解决了我编码重复过多的问题.我不确定这是否是在将我的脚本放入包时通过 CRAN 检查的好方法.等我有结论再更新.
Update 2 @G. Grothendieck posted two approaches. The second one is changing the function environment inside a function. This solves my problem of too many coding replicates. I am not sure if this is a good method to pass through the CRAN check when making my scripts into a package. I will update again when I have some conclusions.
更新
我正在尝试将大量输入参数变量传递给 f2
并且不想将函数内的每个变量索引为 env$c, env$d, env$calls
,这就是为什么我尝试在 f5
和 f6
(修改后的 f2
)中使用 with
.但是,assign
不适用于 {}
内的 with
,将 assign
移至 with
将完成这项工作,但在我的实际情况中,我在 with
表达式中有一些 assign
,我不知道如何将它们移出 用
轻松实现功能.
I am trying to pass a lot of input argument variables to f2
and do not want to index every variable inside the function as env$c, env$d, env$calls
, that is why I tried to use with
in f5
and f6
(a modified f2
). However, assign
does not work with with
inside the {}
, moving assign
outside with
will do the job but in my real case I have a few assign
s inside the with
expressions which I do not know how to move them out of the with
function easily.
这是一个例子:
## In the <environment: R_GlobalEnv>
a <- 1
b <- 2
f1 <- function(){
c <- 3
d <- 4
f2 <- function(P){
assign("calls", calls+1, inherits=TRUE)
print(calls)
return(P+c+d)
}
calls <- 0
v <- vector()
for(i in 1:10){
v[i] <- f2(P=0)
c <- c+1
d <- d+1
}
return(v)
}
f1()
函数f2
在f1
里面,当f2
被调用时,它会查找变量calls,c,d
在环境 environment(f1)
中.这就是我想要的.
Function f2
is inside f1
, when f2
is called, it looks for variables calls,c,d
in the environment environment(f1)
. This is what I wanted.
但是,当我想在其他函数中也使用 f2
时,我将在 Global 环境中定义此函数,将其命名为 f4
.
However, when I want to use f2
also in the other functions, I will define this function in the Global environment instead, call it f4
.
f4 <- function(P){
assign("calls", calls+1, inherits=TRUE)
print(calls)
return(P+c+d)
}
这不起作用,因为它将在全局环境中而不是在调用函数的函数内部查找 calls,c,d
.例如:
This won't work, because it will look for calls,c,d
in the Global environment instead of inside a function where the function is called. For example:
f3 <- function(){
c <- 3
d <- 4
calls <- 0
v <- vector()
for(i in 1:10){
v[i] <- f4(P=0) ## or replace here with f5(P=0)
c <- c+1
d <- d+1
}
return(v)
}
f3()
安全的方式应该是在f4
的输入参数中定义calls,c,d
,然后将这些参数传递给f4
.但是,在我的例子中,有太多的变量要传递到这个函数 f4
中,我可以将它作为环境传递并告诉 f4
不要查看全局环境(environment(f4)
),仅在调用 f3
时查看 environment
内部.
The safe way should be define calls,c,d
in the input arguments of f4
and then pass these parameters into f4
. However, in my case, there are too many variables to be passed into this function f4
and it would be better that I can pass it as an environment and tell f4
do not look in the Global environment(environment(f4)
), only look inside the environment
when f3
is called.
我现在解决的方法是把环境当成列表,使用with
函数.
The way I solve it now is to use the environment as a list and use the with
function.
f5 <- function(P,liste){
with(liste,{
assign("calls", calls+1, inherits=TRUE)
print(calls)
return(P+c+d)
}
)
}
f3 <- function(){
c <- 3
d <- 4
calls <- 0
v <- vector()
for(i in 1:10){
v[i] <- f5(P=0,as.list(environment())) ## or replace here with f5(P=0)
c <- c+1
d <- d+1
}
return(v)
}
f3()
但是,现在 assign("calls", calls+1, inherits=TRUE)
不能正常工作,因为 assign
不会修改原始对象.变量 calls
连接到目标函数为 f5
的优化函数.这就是我使用 assign
而不是传递 calls
作为输入参数的原因.我也不清楚使用 attach
.这是我纠正 assign
问题的方法:
However, now assign("calls", calls+1, inherits=TRUE)
does not work as it should be since assign
does not modify the original object. The variable calls
is connected to an optimization function where the objective function is f5
. That is the reason I use assign
instead of passing calls
as an input arguments. Using attach
is also not clear to me. Here is my way to correct the assign
issue:
f7 <- function(P,calls,liste){
##calls <<- calls+1
##browser()
assign("calls", calls+1, inherits=TRUE,envir = sys.frame(-1))
print(calls)
with(liste,{
print(paste('with the listed envrionment, calls=',calls))
return(P+c+d)
}
)
}
########
##################
f8 <- function(){
c <- 3
d <- 4
calls <- 0
v <- vector()
for(i in 1:10){
##browser()
##v[i] <- f4(P=0) ## or replace here with f5(P=0)
v[i] <- f7(P=0,calls,liste=as.list(environment()))
c <- c+1
d <- d+1
}
f7(P=0,calls,liste=as.list(environment()))
print(paste('final call number',calls))
return(v)
}
f8()
我不确定在 R 中应该如何执行此操作.我的方向是否正确,尤其是在通过 CRAN 检查时?有人对此有一些提示吗?
I am not sure how this should be done in R. Am I on the right direction, especially when passing through the CRAN check? Anyone has some hints on this?
推荐答案
(1) 传递调用者的环境. 可以显式传递父环境并索引.试试这个:
(1) Pass caller's environment. You can explicitly pass the parent environment and index into it. Try this:
f2a <- function(P, env = parent.frame()) {
env$calls <- env$calls + 1
print(env$calls)
return(P + env$c + env$d)
}
a <- 1
b <- 2
# same as f1 except f2 removed and call to f2 replaced with call to f2a
f1a <- function(){
c <- 3
d <- 4
calls <- 0
v <- vector()
for(i in 1:10){
v[i] <- f2a(P=0)
c <- c+1
d <- d+1
}
return(v)
}
f1a()
(2)重置被调用函数的环境我们可以在f1b
中重置f2b
的环境,如下图:
(2) Reset called function's environment We can reset the environment of f2b
in f1b
as shown here:
f2b <- function(P) {
calls <<- calls + 1
print(calls)
return(P + c + d)
}
a <- 1
b <- 2
# same as f1 except f2 removed, call to f2 replaced with call to f2b
# and line marked ## at the beginning is new
f1b <- function(){
environment(f2b) <- environment() ##
c <- 3
d <- 4
calls <- 0
v <- vector()
for(i in 1:10){
v[i] <- f2b(P=0)
c <- c+1
d <- d+1
}
return(v)
}
f1b()
(3) 使用 eval.parent(substitute(...)) 的宏 另一种方法是定义一个类似宏的构造,它可以有效地注入 f2c
内联到 f1c1
.这里 f2c
与 f2b
相同,除了 calls <- calls + 1
行(没有 <<-
需要)并将整个主体包装在 eval.parent(substitute({...}))
中.f1c
与 f1a
相同,只是对 f2a
的调用被替换为对 f2c
的调用.
(3) Macro using eval.parent(substitute(...)) Yet another approach is to define a macro-like construct which effectively injects the body of f2c
inline into f1c1
. Here f2c
is the same as f2b
except for the calls <- calls + 1
line (no <<-
needed) and the wrapping of the entire body in eval.parent(substitute({...}))
. f1c
is the same as f1a
except the call to f2a
is replaced with a call to f2c
.
f2c <- function(P) eval.parent(substitute({
calls <- calls + 1
print(calls)
return(P + c + d)
}))
a <- 1
b <- 2
f1c <- function(){
c <- 3
d <- 4
calls <- 0
v <- vector()
for(i in 1:10){
v[i] <- f2c(P=0)
c <- c+1
d <- d+1
}
return(v)
}
f1c()
(4) defmacro 这与上一个解决方案几乎相同,只是它使用 gtools 包中的 defmacro
来定义宏,而不是自己做.(另请参阅 Rcmdr 包以了解另一个 defmacro 版本.)由于 defmacro
的工作方式,我们还必须传递 calls
但由于它是宏而不是函数,这只是告诉它替换 calls
与将 calls
传递给函数不同.
(4) defmacro This is almost the same as the the last solution except it uses defmacro
in the gtools package to define the macro rather than doing it ourself. (Also see the Rcmdr package for another defmacro version.) Because of the way defmacro
works we must also pass calls
but since it's a macro and not a function this just tells it to substitute calls
in and is not the same as passing calls
to a function.
library(gtools)
f2d <- defmacro(P, calls, expr = {
calls <- calls + 1
print(calls)
return(P + c + d)
})
a <- 1
b <- 2
f1d <- function(){
c <- 3
d <- 4
calls <- 0
v <- vector()
for(i in 1:10){
v[i] <- f2d(P=0, calls)
c <- c+1
d <- d+1
}
return(v)
}
f1d()
这篇关于在R中,如何使函数内的变量可用于该函数内的较低级别函数?(with,attach,environment)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!