[R] R and Scheme

Peter Dalgaard p.dalgaard at biostat.ku.dk
Tue Dec 9 07:19:09 CET 2008


Luke Tierney wrote:

>> R does not have macros.
> 
> Those are related -- because of lazy evaluation one does macros are
> not needed to achive semantic goals (see for example tryCatch).  Being
> able to define friendlier syntax would sometimes be nice though (see
> tryCatch again).

Also for some practical purposes. For instance, it is pretty hard to 
loop over a set of variables and get the output labeled with the 
variable names

for (y in list(systbt, diastbt)) plot (age, y)

will (of course) plot both with a y axis labeled "y". A macro version of 
"for" could potentially create the expansion

plot(age, systbt)
plot(age, diastbt)

and evaluate the result. (Presumably, this is already possible with a 
bit of sufficiently contorted code, but there's no user-friendly syntax 
for it that I know of.)

>> In R, most data types (including numeric vectors) do not have a standard
>> external representation which can be read back in without evaluation.
> 
> The default print form is not readable in this sense but dput is
> available for this purpose.

I think the point is that dput() is not always equivalent to the object 
- parsing dput output gives a different object from the original. 
Numeric vectors of length > 1 is the most obvious case, "expression" 
objects another (those can get terribly confusing at times).

We currently have this sort of confusion

 > e <- substitute(x+a,list(a=c(1,2,3)))
 > e2 <- parse(text=deparse(e))[[1]]
 > e2
x + c(1, 2, 3)
 > e[[3]]
[1] 1 2 3
 > e2[[3]]
c(1, 2, 3)

 > dput(e)
x + c(1, 2, 3)
 > dput(e2)
x + c(1, 2, 3)

That is, deparse/reparse of an object can lead to a _different_ object 
that gets dput()'ed indistinguishably.

I have occasionally been wondering whether it would be a good idea to 
have some sort of syntax primitive requesting parse-time evaluation. E.g.

quote(x + .const(c(1,2,3)))

would be equivalent to

substitute(x+a,list(a=c(1,2,3)))




-- 
    O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
   c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
  (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907



More information about the R-help mailing list