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

From: Janko Thyson <janko.thyson.rstuff_at_googlemail.com>
Date: Wed, 24 Aug 2011 00:02:37 +0200

ink1">ink1">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", plugin="punct"),
     function(src, link, plugin, ...){
         out <- gsub("[[:punct:]]", "", src)


setMethod(f="plugin", signature=signature(src="character", link="foo", plugin="digit"),
     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")

R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Tue 23 Aug 2011 - 22:04:21 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 24 Aug 2011 - 08:30:24 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive