Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6772181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:31:11+00:00 2026-05-26T15:31:11+00:00

this may seem like a overly complicated question, but it has me driving me

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:31:11+00:00Added an answer on May 26, 2026 at 3:31 pm

    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')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this may seem like a very simple question, but I want to dump some
This may seem like a silly question, but is there some way to determine
This may seem like a daft question, but i was wondering about how to
This may seem like a dumb question, but can an app build with c#
This may seem like a simple question but i am getting an error when
This may seem like a stupid question, but what message do i send to
This may seem like a silly question but I can't figure it out. let's
Hi this may seem like a weird question, but here's my situation: I have
Now this may seem like a silly question, but I need to know how
this may seem like a stupid question, but it is stumping me nontheless. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.