s-news
[Top] [All Lists]

Re: Retrieving a data frame name as a character string

To: "Data Analytics Corp." <dataanalytics@earthlink.net>, <s-news@lists.biostat.wustl.edu>
Subject: Re: Retrieving a data frame name as a character string
From: "William Dunlap" <wdunlap@tibco.com>
Date: Tue, 10 Feb 2009 09:46:25 -0800
In-reply-to: <499183BA.40006@earthlink.net>
References: <499183BA.40006@earthlink.net>
Thread-index: AcmLhRXAWaZdbX+CQjSoXNfmhRDSigAIKHOA
Thread-topic: [S] Retrieving a data frame name as a character string
The basic answer is to use
   zname <- deparse(substitute(z)) # z was argument

I find it best if this is used as the default value
of a new argument to your function, as in
   f <- function(z, zname=deparse(substitute(z))) { zname }
Then when you call f from another function you can pass
along the name that you want, as in
   fCaller <- function(arg, argname = deparse(substitute(log(arg)))) {
      f(z=log(arg), zname = argname)
   }
   > fCaller(1:10)
   [1] "log(1:10)"
   > fCaller(log(1:10), "log sequence")
   [1] "log sequence"

Sometimes it also helps to pass along substitute(arg), an expression,
instead of the text strings given by deparse(substitute(arg)).

Also, if you use deparseText(substitute(arg),40) it will output at
most 40 characters of deparsed text as a single string.  That is
handy when the call is by value instead of by name, as in 
   > do.call("fCaller",list(1:30))
   [1] "log(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,"
   [2] "\t13, 14, 15, 16, 17, 18, 19, 20, 21, 22,"
   [3] "\t23, 24, 25, 26, 27, 28, 29, 30))"
or when the actual argument is sufficiently complicated.

Bill Dunlap
TIBCO Software Inc - Spotfire Division
wdunlap tibco.com  

> -----Original Message-----
> From: s-news-owner@lists.biostat.wustl.edu 
> [mailto:s-news-owner@lists.biostat.wustl.edu] On Behalf Of 
> Data Analytics Corp.
> Sent: Tuesday, February 10, 2009 5:40 AM
> To: s-news@lists.biostat.wustl.edu
> Subject: [S] Retrieving a data frame name as a character string
> 
> Hi,
> 
> I have a simple function that has as an argument a data frame name.  
> Inside the function, I use the data frame for various 
> calculations.  But 
> I'd also like to convert that data frame name to a character string.  
> For example, I have
> 
> x <- function(z){ x <- z; ## use z in calculations; ## now 
> convert z to 
> a string}
> 
> x{data.fe)
> 
> What S+ function does this?
> 
> Thanks,
> 
> Walt
> 
> 
> 
> -- 
> ________________________
> 
> Walter R. Paczkowski, Ph.D.
> Data Analytics Corp.
> 44 Hamilton Lane
> Plainsboro, NJ 08536
> ________________________
> (V) 609-936-8999
> (F) 609-936-3733
> dataanalytics@earthlink.net
> www.dataanalyticscorp.com
> 
> --------------------------------------------------------------------
> This message was distributed by s-news@lists.biostat.wustl.edu.  To
> unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
> the BODY of the message:  unsubscribe s-news
> 

<Prev in Thread] Current Thread [Next in Thread>