[R] "registering" a generic method for a class

m.u.r. mmuurr at gmail.com
Fri Jan 2 17:26:25 CET 2009


perfect... I thought it was "print".  thanks much!



On Jan 2, 3:13 am, Matthias Kohl <Matthias.K... at stamats.de> wrote:
> In case of S4 objects "show" is called. Hence, you should implement new
> S4-methods for show ...
>
> Matthias
>
>
>
> m.u.r. wrote:
> > Sorry in advance if this is too simple a question, but I'm stuck with
> > some odd behavior and I can't find the text to rid myself of this
> > (admittedly somewhat trivial) problem.  Note that I've done generic
> > programming with S3 "objects" in the past, but I've never really
> > dabbled in creating S4 objects until now.
>
> > So, I've created a new S4 class, call it "myclass", e.g. :
>
> >> setClass("myclass", representation(slot1 = "list"));
>
> > And I've also created an implementation of "print" for this class via:
>
> >> setMethod("print", "myclass", function(x) { print(paste("a myclass instance with a list of length", length(x at slot1), "in slot1")); });
>
> > Now I can create a new object, say via:
>
> >> myobj <- new("myclass", slot1 = list());
>
> > The question now is the difference in output when I type the object
> > name alone on the command prompt vs when I type print(myobj) on the
> > command prompt, as seen below:
>
> >> myobj
>
> > An object of class “myclass”
> > Slot "slot1":
> > list()
>
> >> print(myobj)
>
> > [1] "a myclass instance with a list of length 0 in slot1"
>
> > I thought that the "print" method is called when an object is called
> > from the command line, and to check this I checked the registered
> > implementations of "print" via:
>
> >> methods("print")
>
> > and in the long output I didn't see the "print.myclass" version.  I
> > had thought that the setMethod method would essentially create this
> > function for me... but I must be missing a key step somewhere along
> > the line.  Can anyone help me figure out what I would need to do to
> > get the generic version of the method (in this toy example, "print")
> > to see my class' method as an implementation of the generic?  Thanks
> > much for any help!
>
> > ______________________________________________
> > R-h... at r-project.org mailing list
> >https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
> --
> Dr. Matthias Kohlwww.stamats.de
>
> ______________________________________________
> R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list