Search code examples
robjectsummaryr-s4

how to edit the codes for the summary of R S4 Object?


I recently want to release a new pacakge with S4 objects. When i get the objects, the slots will be shown. The results list were too large. I would like to consult how to get the the summary of R S4 Object?

just list this:

objects

description information

xxxx xxx

metaData ...

but not this:

objects

slot 1 slot 2 ...

Thanks! Hees


Solution

  • You will need to write methods for the functions print, show and summary if you want custom behaviour for your S4 objects. For example

    ## define a simple class for example purposes
    myClass <- setClass("myclass",slots=c(x="numeric"))
    ## now write a method for the print method that uses my class
    setMethod("print","myclass",function(x)cat("a myclass object with value ",x@x))
    ## make an example object
    z <- myClass(x=1)
    ## now print it --- it uses the method defined above
    print(z)
    

    show is what gets called implicitly when you just type the name of the object at the command line. print and summary are usually called explicitly by the user.