[Rd] Implementing a "plugin" paradigm with R methods

Janko Thyson janko.thyson.rstuff at googlemail.com
Wed Aug 24 00:02:37 CEST 2011

Dear list,

I was wondering how to best implement some sort of a "plugin" paradigm 
using R methods and the dispatcher:
Say we have a function/method ('foo') that does something useful, but 
that should be open for extension in ONE specific area by OTHERS using 
my package. Of course they could go ahead and write a whole new 'foo' 
method including the features they'd like to see, but that's not really 
necessary. Rather, they should be able to just write a new "plugin" 
method for that part of 'foo' that I'd like to open for such plugins.

The way I chose below works, but generates warnings as my method has 
signature arguments that don't correspond to formal classes (which is 
totally fine). Of course I could go ahead and make sure that such 
"dummy" classes exist, but I was wondering if there's a better way.

It'd be great if anyone could let me know how they handle "plugin" 
scenarios based on some sort of method dispatch!


##### CODE EXAMPLE #####

setGeneric(name="foo", signature=c("src"), function(src, ...) 
setGeneric(name="plugin", signature=c("src", "link", "plugin"),
     function(src, link, plugin, ...) standardGeneric("plugin")
setMethod(f="plugin", signature=signature(src="character", link="foo", 
     function(src, link, plugin, ...){
         out <- gsub("[[:punct:]]", "", src)
setMethod(f="plugin", signature=signature(src="character", link="foo", 
     function(src, link, plugin, ...){
         out <- gsub("[[:digit:]]", "", src)
setMethod(f="foo", signature=signature(src="character"),
     function(src, plugin=NULL, ...){
                 signature=c(src=class(src), link="foo", plugin=plugin)
                 stop("Invalid plugin")
             .plugin <- selectMethod(
                 signature=c(src=class(src), link="foo", plugin=plugin),
                 useInherited=c(src=TRUE, plugin=FALSE)
             out <- .plugin(src=src)
         } else {
             out <- paste("Hello world: ", src, sep="")
foo(src="Teststring:-1234_56/", plugin="punct")
foo(src="Teststring:-1234_56/", plugin="digit")

More information about the R-devel mailing list