Accessing variables in a function within a function

你。 提交于 2019-12-06 23:59:34

问题


When running a function in R, I run another function within it. I have a code on the lines of this:

f_a <- function(b, c){
    return(b + c)
}

f_e <- function(){
    b = 2
    c = 2 
    d = f_a(b, c)
    print(d)
}

This works fine. What I'd like to do is not pass the variables b, c to the function f_a. I'd like to do something like this (which throws errors)

f_a <- function(){
    return(b + c)
}

f_e <- function(){
    b = 2
    c = 2
    d = f_a()
    print(d)
}

Is there a way to do this using environments or search paths or any other way?


回答1:


I do encourage you to read about lexical scoping, but I think a good approach to avoid writing a lot of variables could be:

get_args_for <- function(fun, env = parent.frame(), inherits = FALSE, ..., dots) {
    potential <- names(formals(fun))

    if ("..." %in% potential) {
        if (missing(dots)) {
            # return everything from parent frame
            return(as.list(env))
        }
        else if (!is.list(dots)) {
            stop("If provided, 'dots' should be a list.")
        }

        potential <- setdiff(potential, "...")
    }

    # get all formal arguments that can be found in parent frame
    args <- mget(potential, env, ..., ifnotfound = list(NULL), inherits = inherits)
    # remove not found
    args <- args[sapply(args, Negate(is.null))]
    # return found args and dots
    c(args, dots)
}

f_a <- function(b, c = 0, ..., d = 1) {
    b <- b + 1
    c(b = b, c = c, d = d, ...)
}

f_e <- function() {
    b <- 2
    c <- 2
    arg_list <- get_args_for(f_a, dots = list(5))
    do.call(f_a, arg_list)
}

> f_e()
b c d   
3 2 1 5 

Setting inherits = FALSE by default ensures that we only get variables from the specified environment. We could also set dots = NULL when calling get_args_for so that we don't pass all variables, but leave the ellipsis empty.

Nevertheless, it isn't entirely robust, because dots is simply appended at the end, and if some arguments are not named, they could end up matched by position. Also, if some values should be NULL in the call, it wouldn't be easy to detect it.


I would strongly advise against using these below inside an R package. Not only will it be rather ugly, you'll get a bunch of notes from R's CMD check regarding undefined global variables.

Other options.

f_a <- function() {
    return(b + c)
}

f_e <- function() {
    b <- 2
    c <- 2
    # replace f_a's enclosing environment with the current evaluation's environment
    environment(f_a) <- environment()
    d <- f_a()
    d
}

> f_e()
[1] 4

Something like the above probably wouldn't work inside an R package, since I think a package's functions have their enclosing environments locked.

Or:

f_a <- function() {
    with(parent.frame(), {
        b + c
    })
}

f_e <- function() {
    b <- 2
    c <- 2
    f_a()
}

> f_e()
[1] 4

That way you don't modify the other function's enclosing environment permanently. However, both functions will share an environment, so something like this could happen:

f_a <- function() {
    with(parent.frame(), {
        b <- b + 1
        b + c
    })
}

f_e <- function() {
    b <- 2
    c <- 2
    d <- f_a()
    c(b,d)
}

> f_e()
[1] 3 5

Where calling the inner function modifies the values in the outer environment.

Yet another option that is a bit more flexible, since it only modifies the enclosing environment temporarily by using eval. However, there are certain R functions that detect their current execution environment through "daRk magic", and cannot be fooled by eval; see this discussion.

f_a <- function() {
    b <- b + 1
    b + c
}

f_e <- function() {
    b <- 2
    c <- 2
    # use current environment as enclosing environment for f_a's evaluation
    d <- eval(body(f_a), list(), enclos=environment())
    c(b=b, d=d)
}

> f_e()
b d 
2 5 



回答2:


One option is to explicitly grab a and b from the calling environment:

f_a <- function(){
    get('b', envir = parent.frame()) + get('c', envir = parent.frame())
}

f_e <- function(){
    b = 2
    c = 2
    d = f_a()
    d
}

f_e()
#> [1] 4

Alternatively, you can use quote to delay evaluation and then eval to evaluate the code in the calling environment, effectively doing the same thing:

f_a <- function(){
    eval(quote(b + c), parent.frame())
}

This is not really a robust way to write code, though, as it limits the possible ways to call f_a successfully. It's much easier to follow code that explicitly passes variables.




回答3:


Edit:

@alistaire's suggestion to use quote to construct the expressions brings up this further alternative that seems even less ugly:

expr_env <- new.env()
   expr_env$f_a <- quote(b+c)
   expr_env$f_z <- quote(x+y)

f_e<-function(){
    b=2
    c=2
    d=eval( expr_env$f_a)
    print(d)
}

Would defining the function using local be an acceptable alternative?

 f_e<-function(){
     b=2
     c=2
     d<-local({
          b+c
              })

     print(d)
 }
 f_e()
[1] 4

An alternative would be to only return a parse tree and then finish the evaluation in the environment "local" to the function. This seems "ugly" to me:

expr_list<-function(){  f_a <- quote(b+c)
                        f_z <- quote(x+y)
list(f_a=f_a,f_z=f_z) }

f_e<-function(){
    b=2
    c=2
    d=eval( (expr_list()$f_a))
    print(d)
}



回答4:


You can assign the variables to global environment and use inside function.

f_a <- function(){
    return(b + c)
}

f_e <- function(){
    assign("b", 2, envir = .GlobalEnv)
    assign("c", 2, envir = .GlobalEnv)
    d = f_a()
    print(d)
}

# > f_e()
# [1] 4


来源:https://stackoverflow.com/questions/51002811/accessing-variables-in-a-function-within-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!