Re: [S] deparse+substitute

Bill Venables (wvenable@attunga.stats.adelaide.edu.au)
Thu, 23 Apr 1998 18:51:41 +0930


Paul Quataert writes:
> 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.

As one would expect, of course. That information is not passed on.

> Does there exist a DIRECT way to fix this problem ?

I'm going to stick my neck out and say I suspect it is not
possible, for the simple reason that S has a strict call-by-value
protocol. The name of the object is not transmitted to the
function so it cannot be transmitted further to another function.

Given that you can always reach up to the parent frame and indeed
to its parent, and so forth, you can often apparently bypass this
rule, as, for example fix() does. It effectively has a
call-by-name protocol, but it, too, only works at one frame apart
from the object.

> 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).

What you have done here is grab the object name passed as the xv
argument to frame 2 and passed that name object on to frame 3.
It won't work if tstS is actually invoked at frame 4, of course.
(I think it confirms my point, even though it seems to refute
it.)

> Thanks for any help.

Hmm. You may wish to reconsider that. At best I have saved you
some time, at worst I have misled you. :-)

-- 
Bill Venables, Head, Dept of Statistics,    Tel.: +61 8 8303 5418
University of Adelaide,                     Fax.: +61 8 8303 3696
South AUSTRALIA.     5005.   Email: Bill.Venables@adelaide.edu.au

----------------------------------------------------------------------- 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