stevec
stevec

Reputation: 52887

How to see available parameters and documentation for a class method in R?

How can we see all available parameters (or view documentation more generally) for a class method?

For example, if we look at arguments for print()

?print

x   
an object used to select a method.

... 
further arguments passed to or from other methods.

quote   
logical, indicating whether or not strings should be printed with surrounding quotes.


-- leaving others out for brevity --
 
useSource   
logical indicating if internally stored source should be used for printing when present, e.g., if options(keep.source = TRUE) has been in use.

Note that we do not see any documentation for the parameter max_n.

Now suppose we call print() on something of class xml_nodes, e.g:

library(rvest)
library(dplyr)

# Generate an object of class xml_nodes
a <- rep("<p></p>", 30) %>% 
  paste0(collapse="") %>% 
  read_html %>% 
  html_nodes("p")

class(a)
# [1] "xml_nodeset"

a is of class xml_nodeset, and if we call print(a), it prints only 20 results, and that's because (I think) the xml_nodeset class is configured such that when print is called on it, it will only return 20 results. (the '20' number can be changed via the max_n parameter).

How do we find the specific documentation for how print will behave when called on the object of class xml_nodeset? (preferably via RStudio/manuals)

Note that the example above is just a random example, I would like to find a general way of finding documentation for all class methods

Upvotes: 0

Views: 288

Answers (1)

MrFlick
MrFlick

Reputation: 206566

You can see all the "special" version of print by running methods(print). These versions are typically in the form <function name>.<class name>. Many listed there have astericks which means they are not directly exported from the packages where they are defined. If they have documentation, you can access it via ?print.rle for example. In this case there is no documentation for the print.xml_nodeset function. But you can look at it if you do getAnywhere(print.xml_nodeset) or if you happened to know it was from the xml2 namespace, you could do xml2:::print.xml_nodeset (with three colons).

There's also the sloop package which can tell you which S3 method will be called for a given invocation. For example

sloop::s3_dispatch(print(a))
=> print.xml_nodeset
 * print.default

You could file an issue with the package maintainer asking to provide documentation for the function, but otherwise R can't really give you documentation if the author did not include it.

Upvotes: 1

Related Questions