I have the following context:
I do overload cor base function so that I have in my package .R file following statement:
#'export
setGeneric("cor")
Now I want to create a specific function for my objects (class named stranger) -- here for simplicity I just consider my object is a data.table with an additional column named .id.
#' Correlation for stranger objects
#' describeIn cor Correlation method for stranger objects.
setMethod("cor",signature(x="stranger"),function(x, method = c("pearson", "kendall", "spearman")){
selectMethod("cor","ANY")(x[,-'.id',with=FALSE],y=NULL, use="everything",method=method)
})
If I understant setGeneric, it relies on S4 classes -- hence the signature parameter.
However, I don't use S4 classes but build my stranger object with simple old way:
buildClass <- function(x,...){
#... prepare out object as data.table with .ìd column
class(out) <- c("stranger", class(out))
return(out)
}
That is, I don't have S4 classes for my object.
Dispacthing still works: calling cor on my objects correctly apply the dedicated method.
My question is about properly documenting that with ROxygen2. Currently, when loading my functions, I encounter following message:
Updating stranger documentation
Loading stranger
Creating a generic function for 'cor' from package 'stats' in package 'stranger'
in method for 'cor' with signature 'x="stranger"': no definition for class "stranger"
I have already carefully read Hadley vignette on roxygen2 plus some questions that seem related on stackoverflow but they only deal either with classical S3 mechanism or pure S4 whereas I don't have a S4 constructor with setClass and setGeneric relies on S4.
Rather than set an S4 generic method for
cor(), you can redefine it as an S3 generic and define methods for it. To illustrate, I created an R package with only two R files, "buildClass.R", and "cor.R", reproduced below:buildClass.R:
cor.R
Then, if you load your package (named in my case "anRpackage"), the user will be warned that the package masks
stats::cor, but the waycor.default()is defined,stats::cor()is called for objects that are not of classstranger:When checking the package with
devtools::check()using the defaultcran = TRUE(which checks "using the same settings as CRAN uses"), no errors, warnings, or notes were raised: