[Rd] Namespaces and S4 Generics
John Chambers
jmc at r-project.org
Sat Jul 12 03:35:49 CEST 2014
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:
> require(PkgA)
Loading required package: PkgA
> image
function (x, ...)
UseMethod("image")
<bytecode: 0x7fa835170b08>
<environment: namespace:graphics>
> showMethods("image")
Function "image":
<not an S4 generic function>
> showStuff
function ()
showMethods("image")
<environment: namespace:PkgA>
> showStuff()
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:
> I've installed R-devel
>
> R Under development (unstable) (2014-07-09 r66111)
> Platform: x86_64-apple-darwin13.1.0 (64-bit)
>
> and am trying to resolve some problems that I am seeing with my
> SparseM package. In prior versions I explicitly had:
>
> setGeneric("image", function(x, ...) standardGeneric("image"))
>
> and then used setMethod to define a method for the class matrix.csr
> but my reading of the setGeneric manpage suggests that this call to
> setGeneric need not, and probably should not have this second
> argument, and indeed, need not exist at all, since the generic is
> automatically created by the setMethod invocation.
>
> However, what is puzzling to me is that in my new R-devel version
> none of these options have the intended effect of making "image"
> a generic and allowing R to dispatch for matrix.csr objects. When
> I omit the setGeneric() call, I do get a message at INSTALL time
> that a generic for image is being created, but then after loading
> SparseM:
>
>> showMethods(image)
> Function "image":
> <not an S4 generic function>
>
> On the command line, I can make this work:
>
>> setGeneric("image")
> [1] "image"
>> showMethods(image)
> Function: image (package graphics)
> x="ANY"
> x="matrix.csr"
>
> And -- even more irritating -- if I require(Matrix) then the generic
> is created together with my method and all the Matrix methods. At
> the risk of theorizing without data, I suspect that this has something
> to do with NAMESPACES, and I have tried to follow the setup for
> Matrix by having:
>
> importFrom("graphics", image)
> exportMethods("image")
>
> but clearly I'm still missing some crucial aspect. Any suggestions
> would be most welcome.
>
> Roger
>
>
>
> url: www.econ.uiuc.edu/~roger Roger Koenker
> email rkoenker at uiuc.edu Department of Economics
> vox: 217-333-4558 University of Illinois
> fax: 217-244-6678 Urbana, IL 61801
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
More information about the R-devel
mailing list