Re: [S] deparse+substitute: substitute.recurse()

Jens Oehlschlaegel (oehl@Psyres-Stuttgart.DE)
Tue, 28 Apr 1998 22:20:31 +0200 (MET DST)


Dear Paul,

Is the following what you were looking for?
Any comments welcome.

Best regards

Jens Oehlschlaegel

substitute.recurse <- function(arg, up.limit=nframe, rather.name=T,
nframe=sys.nframe()-1){
# finds argument names up to any possible frame
# up.limit restricts recursion depth
# substitute.recurse(x, up.limit=1) behaves like substitute(x)
# recursion stops also if an argument specification in a call
!is.name()
# rather.name determines whether in this case the non-name or the
last name is returned
# (c) JOA 1998
if (nframe==sys.nframe()-1) arg <- substitute(arg) # first call is
different
arg.new <- eval(parse(text = paste("substitute(",deparse(arg),")",
sep="")), local = nframe)
if (is.name(arg.new) && nframe>1 && up.limit>1) arg.new <-
Recall(arg.new, nframe=nframe-1, up.limit=up.limit-1,
rather.name=rather.name)
if (is.name(arg.new) || !rather.name) arg <- arg.new
arg
}

# functions for examples
tt1 <- function(x, up.limit=3, rather.name=T)tt2(x, up.limit=up.limit,
rather.name=rather.name)
tt2 <- function(y, up.limit=up.limit, rather.name=rather.name)tt3(y,
up.limit=up.limit, rather.name=rather.name)
tt3 <- function(z, up.limit=up.limit,
rather.name=rather.name)substitute.recurse(z, up.limit=up.limit,
rather.name=rather.name)
a <- 7

# examples
> tt1(a)
a
> tt1(6)
x
> tt1(6, rather.name=F)
[1] 6
> tt1(up.limit=1)
y

# example like original question
> tstS <- function(xv)deparse(substitute.recurse(xv))
> tstM <- function(...) tstS(...)
> tstM(xv=letters)
[1] "letters"


Paul Quataert <Paul.Quataert@ihe.be> asked:

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

--
Jens Oehlschlaegel-Akiyoshi
Psychologist/Statistician
Project TR-EAT + COST Action B6
                                                 F.rankfurt
oehl@psyres-stuttgart.de                         A.ttention
+49 711 6781-408 (phone)                         I.nventory
+49 711 6876902  (fax)                           R .-----.
                                                  / ----- \
Center for Psychotherapy Research                | | 0 0 | |
Christian-Belser-Strasse 79a                     | |  ?  | |
D-70597 Stuttgart Germany                         \ ----- /
-------------------------------------------------- '-----' -
(general disclaimer)                             it's better

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