[R] NextMethod() example from S Programming by Venables and Ripley (page 78)
Ken Knoblauch
ken.knoblauch at inserm.fr
Sun Feb 14 08:00:20 CET 2010
blue sky <bluesky315 <at> gmail.com> writes:
> S Programming by Venables and Ripley (page 78) has the example listed
> at the end of this email. However, I get the following error when I
> try the example. I don't understand the descriptions of NextMethod on
> its help page. Could somebody let me know how to fix the error of this
> example?
> > test(x)
> c1
> c2
> Error in NextMethod() : no method to invoke
> Calls: test -> test.c1 -> NextMethod -> test.c2 -> NextMethod
> Execution halted
>
> ####
> test=function(x) UseMethod('test')
>
> test.c1=function(x) {
> cat('c1\n')
> NextMethod()
> x
> }
>
> test.c2=function(x) {
> cat('c2\n')
> NextMethod()
> x
> }
>
> test.c3=function(x) {
> cat('c3\n')
> x
> }
>
> x=1
> class(x)=c('c1','c2')
> test(x)
It works fine for me if you add a default method,
which I think is what it is looking for.
test.default <- function(x){
cat("default")
x
}
test(x)
c1
c2
default[1] 1
attr(,"class")
[1] "c1" "c2"
--
Ken Knoblauch
Inserm U846
Stem-cell and Brain Research Institute
Department of Integrative Neurosciences
18 avenue du Doyen Lépine
69500 Bron
France
tel: +33 (0)4 72 91 34 77
fax: +33 (0)4 72 91 34 61
portable: +33 (0)6 84 10 64 10
http://www.sbri.fr/members/kenneth-knoblauch.html
More information about the R-help
mailing list