0
votes

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 S3methodand 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.

1
See cran.r-project.org/doc/manuals/r-release/… and stackoverflow.com/a/62609615/1412059. You shouldn't need to import print or plot because they are in the base namespace.Roland
Thanks Roland, that is helpful. I tried with the extra export as per the stackoverflow tread, to no avail. I think the problem is I am using R6.2,.0 They have changed the way S3 methods are treated. As if it weren't confusing enough :-(Marco Stamazza
What is "R6.2,.0"? Who is "They"? I'm not aware of relevant changes in R. If you use an older version of roxygen2, you would need @method generic class.Roland
Well, I don't know who decides changes in R, I call them they. Sorry, I meant R 3.6.0. well, it looks like there have been changes in the way S3 methods are handled. Not sure who approves the changes. I am using roxygen 7.1.1. Maybe it is a bug in R 3.6.x. many thanks.Marco Stamazza
The issue is most certainly not with R. If you create the NAMESPACE file manually and correctly, you'll see that it works just fine. Your issue is with (your use of) roxygen2.Roland

1 Answers

0
votes

So, the answer was that i was using the wrong @export directive.

I used @export print.marco. This overrides the creation of S3 methods. Putting simply @export works fine. Thanks to Roland for his comments above.