I am writing a package for my use. I created objects of class marco, say.
Then I wrote some methods, like print.marco
, plot.marco
, etc, which I would like to be applied with print(obj)
where class(obj) = "marco"
. In the NAMESPACE file (created with roxygen2::document()
), these functions are simply exported as such and not as S3method
and are not recognized as such by sloop::is_s3_method
.
I searched the internet and I can't find an answer or clear example. Following In Hadley Wickham's R packages in my R script I simply document the functions adding #' @export print.marco
, etc.
A minimal example
#' Prints a marco object
#' @param marco_obj A marco object.
#' @export print.marco
print.marco(marco_obj){
print(marco_obj$this_is_printable)
}
From the above mentioned book I read (bold mine)
S3 generics are just functions, so the same rules for functions apply. S3 methods always accompany the generic, so as long as you can access the generic (either implicitly or explicitly), the methods will also be available. In other words, you don’t need to do anything special for S3 methods. As long as you’ve imported the generic, all the methods will also be available.
I guess all I need to understand is how to import generics?
Can someone help?
EDIT
The problem was that I used @export print.marco
. This overrides the creation of S3 methods. Putting simply @export
works fine. Thanks to Roland for his comments below.
print
orplot
because they are in the base namespace. – Roland@method generic class
. – Roland