S4 classes in R: printing function definition and getting help

October 4, 2010

(This article was first published on Super Nerdy Cool » R, and kindly contributed to R-bloggers)

I’m not very familiar with S4 classes and methods, but I assume it’s the recommended way to write new packages since it is newer than S3; this of course is open to debate. I’ll outline my experience of programming with S4 classes and methods in a later post, but in the mean time, I want to write down some notes on how to get help (via ? in R) and getting function definitions from S4 methods.

For S3 classes and methods, suppose I want to learn more about a certain method, say print of some class. Let’s use class lm as an example. I could type ?print.lm to get documentation on the function, and type print.lm in the R console to get the function definition printed. This allows me to learn more about the method and learn from them (perks of open source!). To recap:

## S3
?generic.class ## get help to the generic function "generic" of a particular class "class"
generic.class ## print the function in console

However, with S4, this is not the class. I’ve used a few packages that are written in S4 and could not get documentation open within R and get the function definitions printed to learn about the underlying code based on the previous techniques. As I learn to write and document an R package based on S4, I read this section of the R manual for writing packages. I misinterpreted the reading and thought to get help on a method I had to type something like methods?generic,signature_list-method to get help. However, I received an error due to the - symbol (it’s an operator in R). I believe the stated convention is just for the documentation piece of S4 methods in the .Rd files. After some more searching, I came across this link (examples section) that showed me how to get help. Let’s illustrate with the show method (S4′s equivalent of the print method) for the mer class in the lme4 package.

## S4
showMethods("show") ## show all methods for show
?show ## shows the generic documentation of show
method?show("mer") ## method?generic("signature 1", "signature 2", ...) -- get help for the generic function for a particular signature list, usually a single class
getMethod("show", signature="mer") ## function definition
lme4:::printMer ## printMer is what the show method for mer calls

For our particular example, the show method for the mer class calls a printMer function in the lme4 namespace. Thus, we need to call lme4:::printMer to see the definition.

Hope this out others out there.

To leave a comment for the author, please follow the link and comment on their blog: Super Nerdy Cool » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Mango solutions

RStudio homepage

Zero Inflated Models and Generalized Linear Mixed Models with R

Quantide: statistical consulting and training



CRC R books series

Contact us if you wish to help support R-bloggers, and place your banner here.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)