13

this may seem like a overly complicated question, but it has me driving me a little nuts for some time. It is also for curiosity, because I already have a way of doing what I need, so is not that important.

In R, I need a function to return a named list object with all the arguments and the values entered by the user. For this I have made this code (toy example):

foo <- function(a=1, b=5, h='coconut') {
    frm <- formals(foo)
    parms <- frm
    for (i in 1:length(frm))
        parms[[i]] <- get(names(frm)[i])
    return(parms)
}

So when this is asked:

> foo(b=0)

$a
[1] 1

$b
[1] 0

$h
[1] "coconut"

This result is perfect. The thing is, when I try to use lapply to the same goal, so as to be a little more efficient (and elegant), it does not work as I want it to:

foo <- function(a=1, b=5, h='coconut') {
    frm <- formals(foo)
    parms <- lapply(names(frm), get)
    names(parms) <- names(frm)
    return(parms)
}

The problem clearly is with the environment in which get evaluates it's first argument (a character string, the name of the variable). This I know in part from the error message:

> foo(b=0)
Error in FUN(c("a", "b", "h")[[1L]], ...) : object 'a' not found

and also, because when in the .GlobalEnv environment there are objects with the right names, foo returns their values instead:

> a <- 100
> b <- -1
> h <- 'wallnut'
> foo(b=0)
$a
[1] 100

$b
[1] -1

$h
[1] "wallnut"

Obviously, as get by default evaluates in the parent.frame(), it searches for the objects in the .GlobalEnv environment, instead of that of the current function. This is strange, since this does not happen with the first version of the function.

I have tried many options to make the function get to evaluate in the right environment, but could not do it correctly (I've tried pos=-2,0,1,2 and envir=NULL as options).

If anyone happen to know a little more than me about environments, specially in this "strange" cases, I would love to know how to solve this.

Thanks for your time,

Juan

Juan
  • 1,351
  • 1
  • 14
  • 28

3 Answers3

11

Edit of 2013-08-05

Using sapply() instead of lapply(), simplifies this considerably:

foo4 <- function(a=1, b=5, h='coconut') {
    frm <- formals(sys.function())
    sapply(names(frm), get, envir=sys.frame(sys.parent(0)), simplify=FALSE)
}
foo4(b=0, h='mango')

This, though, without sapply() or lapply() might be the more elegant solution:

foo5 <- function(a=1, b=5, h='coconut') {
    modifyList(formals(sys.function()), as.list(match.call())[-1])
}
foo5(b=0, h='mango')

Original post (2011-11-04)

After casting about a bit, this looks to be the best solution.

foo <- function(a=1, b=5, h='coconut') {
    frm <- formals(foo)
    parms <- lapply(names(frm), get, envir=sys.frame(sys.parent(0)))
    names(parms) <- names(frm)
    return(parms)
}
foo(b=0, h='mango')
# $a
# [1] 1

# $b
# [1] 0

# $h
# [1] "mango"

There's some subtle stuff going on here with the way that lapply scopes/evaluates the calls that it constructs. The details are hidden in a call to .Internal(lapply(X, FUN)), but for a taste, compare these two calls:

# With function matched by match.fun, search in sys.parent(0)
foo2 <- function(a=1, h='coconut') {
    lapply(names(formals()), 
           get, envir = sys.parent(0))
}

# With anonymous function, search in sys.parent(2)    
foo3 <- function(a=1, h='coconut') {
    lapply(names(formals()), 
           FUN = function(X) get(X, envir = sys.parent(2)))
}

foo4(a=0, h='mango')
foo5(a=0, h='mango')
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • 1
    ...even better if `foo2` and `foo3` got their own formals, and not `foo`'s :-) – Tommy Nov 05 '11 at 00:04
  • 1
    To Josh: would it be the same if I use `envr=sys.parent() + 1`? – Juan Nov 05 '11 at 00:14
  • @Tommy -- it would be even better, wouldn't it. Thanks for pointing that out. I guess `frm <- formals()` is generally even better, but I'll leave it as is to highlight the single important difference. – Josh O'Brien Nov 05 '11 at 01:43
  • @Juan -- You're right. `envir=sys.parent() + 1` works (and so does `envir=sys.parent(0)`). Apparently the `envir` argument to `get` interprets integers as referring to the numbered frames on the calling stack (which you can examine with `sys.frames()`). The `sys.frame()` I put in `foo3` was thus totally unnecessary. – Josh O'Brien Nov 05 '11 at 05:16
  • 1
    @Juan -- Thanks, by the way for the great original post. It drove me nuts too, for several hours there! – Josh O'Brien Nov 05 '11 at 05:19
  • @JoshO'Brien thanks! It was just a problem that I bumped into. And by the way, the strategy used in the original `foo` function (the one with a `for` loop) has it's problems: whenever a default value is `NULL` problems arise...(e.g.: `foo <- function(a=NULL, b=2)`). This does not happen with the `lapply` versions. – Juan Nov 07 '11 at 15:24
  • 1
    Instead of `sys.frame(sys.parent(0))`, I think `environment()` is a bit more obvious. In fact you could replace that entire line with `mget(names(frm), environment())` – hadley Aug 07 '13 at 01:50
  • 1
    Or you could replace the entire function with `as.list(environment())` – hadley Aug 07 '13 at 01:51
7

Just convert the current environment into a list:

foo <- function(a=1, b=5, h='coconut') {
  as.list(environment())
}
foo(a = 0, h = 'mango')
hadley
  • 102,019
  • 32
  • 183
  • 245
1

This is adapted from @Josh O'Brien's solution above using sapply to automatically assign the correct names to the resulting list (saves one line of code):

foo <- function(a=1, b=5, h='coconut') {
    frm <- formals(foo)
    parms <- sapply(names(frm), get, envir=sys.frame(sys.parent(-1)), simplify=FALSE)
    return(parms)
}
Tony Breyal
  • 5,338
  • 3
  • 29
  • 49