[R-pkg-devel] Dispatch for S3 and Ordinary Function with Same Name

Uwe Ligges ||gge@ @end|ng |rom @t@t|@t|k@tu-dortmund@de
Mon Aug 29 18:04:40 CEST 2022



On 29.08.2022 17:00, Dario Strbenac wrote:
> Good day,
> 
> If there is a function named predict in my package and another in another package, such as randomForest, which is also called in a particular wrapper function in my package, how may I ensure that the call to predict inside of the wrapper function will call randomForest's prediction function and not the predict function defined elsewhere in my package? To illustrate
> 
> randomForestPredictInterface <- function(forest, measurementsTest, ...)
> {
>    predictions <- predict(forest, measurementsTest) # Currently, executes predict defined in my package.

Idealy do not define a new predict which is a generic function in  R.

Rather, you methods may produce objects that follow,e g.g., the classes 
provided by randomForest plus eventiually extra elements.
Then you invent a new class name, e.g. moreRandomForest that inherits 
from randomForest:

...
class(x)  <- c("moreRandomForest", "randomForest")

if you provide your own moreRandomForest method for predict, dispatch 
will choose it, if you do not provide it, mathod dispatch will choose 
the one from the pterh package. Please simply read some introductory 
material about S3.


Best,
Uwe Ligges



> --------------------------------------
> Dario Strbenac
> University of Sydney
> Camperdown NSW 2050
> Australia
> ______________________________________________
> R-package-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel



More information about the R-package-devel mailing list