Skip to content
Prev 48619 / 63424 Next

Namespaces and S4 Generics

Roger,

Are you implying that your current code works differently under R-devel than with the current release?  Nothing I'm aware of would suggest that.

With or without setGeneric() (in either version but the single-argument is cleaner), a generic function for image() is created in your package's namespace.

If you don't export that function, calls from other functions in your package will still get the generic  version but calls from outside the package will get graphics::image.

Here's an example.  In my PkgA I have:

	setGeneric("image")

	setMethod("image", "A", function(x, ...) message("dummy image for class A"))

	showStuff <- function() showMethods("image")

In the NAMESPACE, I exported showStuff, but not image.

As a result:
Loading required package: PkgA
function (x, ...) 
UseMethod("image")
<bytecode: 0x7fa835170b08>
<environment: namespace:graphics>
Function "image":
 <not an S4 generic function>
function () 
showMethods("image")
<environment: namespace:PkgA>
Function: image (package graphics)
x="A"
x="ANY"

The methods are visible to code inside PkgA but others see only the S3 function.

John
On Jul 11, 2014, at 11:27 AM, Roger Koenker <rkoenker at illinois.edu> wrote: