3

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.

Marco Stamazza
  • 836
  • 9
  • 15
  • 1
    See https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Registering-S3-methods and https://stackoverflow.com/a/62609615/1412059. You shouldn't need to import `print` or `plot` because they are in the base namespace. – Roland Jul 27 '20 at 09:15
  • 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 Jul 27 '20 at 11:20
  • 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 Jul 27 '20 at 11:47
  • 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 Jul 27 '20 at 12:01
  • 1
    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 Jul 27 '20 at 14:59
  • ok, that's good to know. I hope someone can guess what I am doing wrong. Thanks again. – Marco Stamazza Jul 27 '20 at 23:15
  • Hi Roland. You were absolutely right, it was my use of Roxygen2. The error was ``@export print.marco``. This overrides the creation of an S3 method. Putting simply ``@export`` works like a charm! Thanks for standing your ground and make me look into my code instead of blaming "them" :-) – Marco Stamazza Jul 28 '20 at 22:52

1 Answers1

2

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.

Marco Stamazza
  • 836
  • 9
  • 15