[Rd] Using substitute from inside an S4 method

From: Franck Arnaud <franck.arnaud_at_gmail.com>
Date: Wed 13 Dec 2006 - 21:47:20 GMT


Hi all,
I don't understand why this does not what I expect :

## code start here ##############
setClass("num",representation(x="numeric"))

num<-function(x) new("num",x=x)

add<-function(e1,e2) {

    cat("Computing
",deparse(substitute(e1)),"+",deparse(substitute(e2)),"\n")

    e1@x+e2@x
}

setMethod("+","num",function(e1,e2) {

    cat("Computing
",deparse(substitute(e1)),"+",deparse(substitute(e2)),"\n")

    e1@x+e2@x
})

a<-num(3.2)
b<-num(-1.4)

add(a,b)
a+b
## code ends here ##############

a+b does not work : I would like that add(a,b) and a+b give the exact same result
I've seen a post on R-devel, but the answer seemed not to apply here. I've tried to use deparse(substitute(e1,sys.frame(-1))) and deparse(substitute(e1,sys.frame(-2))) (as it was advised by GG in january 2006). But it did not work.

Therefore, i'm looking for 1) an explanation for this phenomenon (link to a doc, anything) and/or 2) a way to do what i want, if it is possible.

Thanks a lot

        [[alternative HTML version deleted]]



R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Thu Dec 14 19:11:57 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 14 Dec 2006 - 10:30:53 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.