[Rd] Namespaces and S4 Generics
jmc at r-project.org
Sat Jul 12 03:35:49 CEST 2014
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:
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, ...)
<not an S4 generic function>
Function: image (package graphics)
The methods are visible to code inside PkgA but others see only the S3 function.
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
> Function "image":
> <not an S4 generic function>
> On the command line, I can make this work:
>  "image"
> Function: image (package graphics)
> 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)
> but clearly I'm still missing some crucial aspect. Any suggestions
> would be most welcome.
> 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
More information about the R-devel