[R] substitute question
Peter Dalgaard
p.dalgaard at biostat.ku.dk
Thu Mar 18 23:52:47 CET 2004
"Gabor Grothendieck" <ggrothendieck at myway.com> writes:
> From: Peter Dalgaard <p.dalgaard at biostat.ku.dk>
> > (The real pain in these examples is that substitute autoquotes its
> > expr argument. Therefore, when you want to modify an expression that
> > is already stored in a variable, you need an extra outer layer of
> > eval(substitute(...)) to poke the content of the variable into the
> > inner substitute. An "esub" function with standard evaluation
> > semantics would make this much easier.)
>
> That is one of the frustrations of using substitute.
>
> The other is that even if you do perform two levels of substitute,
> as I have been trying, you still can't count on it working for
> an arbitrary unevaluated expression, as my examples show.
Er, I don't think so. All I have seen is a couple of cases where you
tried to pass something that was not a language object (e.g. a
function as opposed to an expression or call generating a function.)
> Even putting aside the source attribute which is super confusing
> until you know about it, all the solutions that I can see to
> the problem I presented are ugly.
>
> (1) One can either pick apart the function using body, or
>
> (2) I assume one could convert the function to text and
> paste together the correct substitute command with the text of
> the function inserted in its argument.
>
> The quote mechanism works but is not applicable if all you have
> is the function itself (as you point out).
>
> This is sooo frustrating.
Well, the certain road to frustration is to try to do the impossible.
A function is not a language object and you can only do substitutions
on language objects.
> f <- function()x
> g <- quote(function()x)
> f
function()x
> g
function() x
> mode(f)
[1] "function"
> mode(g)
[1] "call"
> is.language(f)
[1] FALSE
> is.language(g)
[1] TRUE
However, a function is basically a triplet consisting of an argument
list, a body, and an environment, the middle of which *is* a language
object. So I don't think your (1) is ugly, it's the logical way to
proceed.
--
O__ ---- Peter Dalgaard Blegdamsvej 3
c/ /'_ --- Dept. of Biostatistics 2200 Cph. N
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
More information about the R-help
mailing list