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:
- Which are the differences between
UseMethod
and.S3method()
; - why my method is not exported?
Answer:
as mentioned by @MrFlick,
UseMethod
is used in Packages;.S3method
should only be used in R scripts,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
}