[R] Returning the name of an object passed directly or from a list by lapply

William Dunlap wdunlap at tibco.com
Fri Sep 16 03:03:37 CEST 2011


Is th following the sort of thing you are looking for?

f <- function (...)
{
    unevaluatedArgs <- substitute(...())
    evaluatedArgs <- list(...)
    stopifnot(length(unevaluatedArgs) == length(evaluatedArgs))
    tags <- vapply(unevaluatedArgs, FUN=function(x) deparse(x)[1],
        FUN.VALUE=character(1))
    if (!is.null(tmp <- names(evaluatedArgs))) {
        # if argument is tagged, tag=expr, use the tag
        i <- !is.na(tmp) & tmp != ""
        tags[i] <- tmp[i]
    }
    for (i in seq_along(tags)) {
        cat("***", tags[i], "***\n")
        str(evaluatedArgs[[i]]) # your choice of debug output
    }
}

E.g.,

> f(log(1:4), reciprocals=1/(1:4), list(s=sqrt(1:4),p=c(2,3,5,7)), pi)
*** log(1:4) ***
 num [1:4] 0 0.693 1.099 1.386
*** reciprocals ***
 num [1:4] 1 0.5 0.333 0.25
*** list(s = sqrt(1:4), p = c(2, 3, 5, 7)) ***
List of 2
 $ s: num [1:4] 1 1.41 1.73 2
 $ p: num [1:4] 2 3 5 7
*** pi ***
 num 3.14

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of andrewH
> Sent: Thursday, September 15, 2011 5:36 PM
> To: r-help at r-project.org
> Subject: Re: [R] Returning the name of an object passed directly or from a list by lapply
> 
> Thanks Bill!
> 
> You are correct. I did not understand what was inmy list.
> 
> I posted a simplified example in the hope of focusing on the essentials, but
> I see I have edited out the motivation.  When my programs go awry, and
> sometimes when they don't, I find I need to understand what is in some
> variable or variables.  To help with debugging, I have built a little
> testing function that takes the names of one or more variables and returns a
> variety of information about each one: summary(), str(), class(), type(),
> etc., starting with the name. (The name is unimportant when I hand it one
> variable, but for a longer list, I want to print it to help keep track of
> what outcome goes with what variable).  It also gives me some extra
> information about certain data types that I seem to have more trouble with,
> notably factors. These days I’m devoting myself nearly full-time to trying
> to learn R, and I probably run this function between 50 and 200 times a day.
> 
> Now I am trying to figure out some way of running my testing function on
> more than one variable at a time. Should be easy on a computer, right?  I
> don't care if I cluster my variables is a list, vector, or what -- I just
> want to be able to evaluate a bunch of them at one time. And I'd rather not
> have to type quotation marks around each variable name. I've timed myself,
> and it increases the time it takes me to type a list by 250%. Shortly I'll
> be posting a different question with regards to my failure to get this
> function to work in a loop.  But I also very much want to be able to use one
> of the apply-family functions to run on multiple variables.
> 
> If, as you have persuaded me,  I can not use a list of variable names, this
> larger problem still has to have a straightforward solution, I think. But I
> sure don't know what it is.
> 
> Any suggestions from any quarter would be deeply appreciated.
> 
> andrewH
> 
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/Returning-the-name-of-an-object-passed-
> directly-or-from-a-list-by-lapply-tp3816798p3817116.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


More information about the R-help mailing list