Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using get inside lapply, inside a function

Tags:

r

lapply

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

like image 644
Juan Avatar asked Nov 04 '11 22:11

Juan


People also ask

How do you use functions in Lapply?

Using the lapply function is very straightforward, you just need to pass the list or vector and specify the function you want to apply to each of its elements.

Is Lapply faster than for loop?

The apply functions (apply, sapply, lapply etc.) are marginally faster than a regular for loop, but still do their looping in R, rather than dropping down to the lower level of C code. For a beginner, it can also be difficult to understand why you would want to use one of these functions with their arcane syntax.

Does Lapply always return list?

The result is a list, since lapply() always returns a list. Each element of this list is of length one, so the result can be simplified to a vector by calling sapply() instead of lapply().

How do I apply a function to a list in R?

lapply() function in R Programming Language is used to apply a function over a list of elements. lapply() function is used with a list and performs the following operations: lapply(List, length): Returns the length of objects present in the list, List.


1 Answers

Just convert the current environment into a list:

foo <- function(a=1, b=5, h='coconut') {
  as.list(environment())
}
foo(a = 0, h = 'mango')
like image 99
hadley Avatar answered Oct 08 '22 15:10

hadley