Re: [S] deparse+substitute

Patrick Burns (pburns@pburns.seanet.com)
Wed, 22 Apr 1998 19:37:55 +0000


Paul Quataert wrote:
>
> I want to use the deparse(substitute()) combination
> to create labels, e.g.
> tstS <- function(xv) deparse(substitute(xv))
> Then I get for
> tstS(letters)
> [1] "letters"
>
> Everything is OK for now. However this trick does not
> work anymore when I use tstS as a subroutine in another
> procedure, e.g.
> tstM <- function(...) tstS(...)
> Then I get
> tstM(xv=letters)
> [1] "..1"
> which is the name of the variable in tstM, and not the
> name of the original variable.
>
> Does there exist a DIRECT way to fix this problem ?
>
> NOTE:
> Surely there exist indirect solutions, e.g.
> tstS <- function(xv,xlab=deparse(substitute(xv))) xlab
> tstM <- function(xv,...) tstS(xv,deparse(substitute(xv)))
> tstM(xv=letters)
> "letters"
>
> This works fine, but then I need to define the variable
> xv in the main procedure tstM, something I prefer not
> to do for my application (and anyhow tstM will not work
> anymore when I use this in another procedure).

I'm sure there are better ways, but here is the first one that
worked for me. This technique is going to get pretty messy if
you traverse a few more levels.

> tstM
function(...)
tstS(...)
> tstS
function(xv)
{
deparse(eval(parse(text = paste("substitute(",
deparse(substitute(xv)),
",sys.parent())"))))
}

> tstM(4:6)
[1] "4:6"
> tstM(xv=letters)
[1] "letters"

Pat Burns

-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news