Re: [R] Substituted arguments surviving multiple function calls

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Wed 24 Aug 2005 - 01:10:46 EST

I think you would do better to use [[]], which allows this sort of thing as character strings.

f1 <- function(x) d[[deparse(substitute(x))]]

Now f1 is looking for the *name* it is called with, so you need

f2 <- function(x) eval(substitute(f1(x), list(x=substitute(x))))

Leaving the eval off

f2n <- function(x) substitute(f1(x), list(x=substitute(x)))

> f2n(a1)

f1(a1)

shows you how it works.

This really isn't a good idea, though, just to avoid a couple of quotes.

On Tue, 23 Aug 2005, Michael Hoffman wrote:

> I am using R 2.1.1 and have written a function that will retrieve a
> named column from a data frame:
>
> d = data.frame(a1=c(4, 2), a2=c(6, 7))
> f1 = function(x)
> {
> do.call("$", list(d, substitute(x)))
> }
>
> So this works:
>
> > f1(a1)
> [1] 4 2
>
> However, I want to make another function, f2, which also accepts a
> column name as an argument and then calls the first function with it:
>
> f2 = function(x)
> {
> f1(substitute(x))
> }
>
> However, this does not work:
>
> > f2(a1)
> Error in list(a1 = c(4, 2), a2 = c(6, 7))$substitute(x) :
> invalid subscript type
>
> It works if I take the substitute() out of f1(), but then I can only
> call f1() through f2() or something that does the substitution for it.
> Is there a better way to do this?
>
> Thanks for any help you can offer.
> --
> Michael Hoffman
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Wed Aug 24 01:17:38 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 15:46:41 EST