0

I am new in package development. And I am joining in a package some of my functions I use a lot for personal use... Studying how package is developed, I consider S3 object types interesting... But I am really new on S3 and object-oriented languages and I have doubts in how to use S3 functions...

Does it really suffices to specify

myFunc<-function(data,...){
          UseMethod("myFunc",data)
}
myFunc.MyClass<-function(data){
          # several lines of code here
}

?

It appears to be sufficient when I run the codes, but when I try to "install and restart", this it does not work... By searching on the here, here and here I found the .S3method() function

.S3method("summary","myClass",summary.myClass)

Which solves my problem. But I am not able to understand when it is required.

When should I use .S3method() and methods()? Which are their differences?

The code examples appears to use just one of them, but for me, at least for the "summary" function, is working just when I use the both... Should I add it for all of them?


UPDATE: The @MrFlick not just answered my question, but got all the issue behind my attempt in looking at .S3method() as a way to solve a undescribed issue... After the comments of @MrFlick, I figured out this question can be split in two questions:

  1. Which are the differences between UseMethod and .S3method();
  2. why my method is not exported?

Answer:

  1. as mentioned by @MrFlick, UseMethod is used in Packages; .S3method should only be used in R scripts,

  2. It is somehow a duplicate for previous questions here and here. I imagined exporting the generic (without exporting the class related method) was sufficient...

    @export myFunc<-function(data,...){ UseMethod("myFunc",data) }

    myFunc.MyClass<-function(data){ # several lines of code here }

But no, I need to export both:

@export
myFunc<-function(data,...){
                      UseMethod("myFunc",data)
}
@export
myFunc.MyClass<-function(data){
                  # several lines of code here
}
hamagust
  • 728
  • 2
  • 10
  • 28
  • Did you see the note in the `?.S3method` help page: "This function should only be used in R scripts: for package code, one should use the corresponding S3method ‘NAMESPACE’ directive."? If you are developing packages, make sure to register the generic method in your namespace file. Do not use the `.S3method` function. – MrFlick Jul 14 '20 at 19:54
  • @MrFlick, thank you very much for the quick response. By 'register the generic method in your namespace file' did you mean '@export' it? for all function, not just the generic one? Is it the reason I am not able to run it before? – hamagust Jul 14 '20 at 19:59
  • 1
    Well, `@export` is something that `roxygen` adds. Are you using that? If so then [just use @export](https://stackoverflow.com/a/22598266/2372064) and roxygen should do the work of putting the correct thing in the NAMESPAGE file for you. – MrFlick Jul 14 '20 at 20:02
  • Yes. I am using `roxygen`. Thank you very much. I understand now the problem is in the specification of `@export`... I will verify if the `@export` part is correctly specified for all `summary.class` functions... – hamagust Jul 14 '20 at 20:06

0 Answers0