[R] setMethod call in package code using Rcpp
Pascal A. Niklaus
pascal.niklaus at ieu.uzh.ch
Sun Jan 23 15:41:45 CET 2011
Dear all,
I experience a problem in implementing a S4 class in a package and would
appreciate your help in this matter.
The class is implemented using Rcpp, which works well. I then extend the
class with R functions, adding them as methods with a call to
setMethod(...). When I do this outside the package (after loading the
package with "library(...)"), everything works smoothly. However, when I
try to incorporate the call to setMethod(...) in the package code
itself, the only way I get it to work is to include the call in
.onLoad(...) or .onAttach(...). This works, but when loading the library
I get the messages "creating new generic function for "plot" in
".GlobalEnv", as well as "no definition for class "Rcpp_rothC""...
Again, the code works, but the messages let me presume that I add the
methods in the wrong way or miss-specify name spaces, class names, or
something else.
Thank you for your advice.
Pascal
(The error message and the relevant parts of the package files are
pasted below, together with my related questions as "comments".)
> showMethods("plot")
Function "plot":
<not a generic function> # I am surprised about this --
# why isn't plot a generic function?
> library("RrothC2")
Loading required package: Rcpp
Loading required package: pascal
Creating a new generic function for "plot" in ".GlobalEnv"
in method for ‘plot’ with signature ‘x="Rcpp_rothC",y="missing"’: no
definition for class "Rcpp_rothC"
# class seems not to be known
# at this stage.
# but where should I put setMethod()?
> r1 <- new(rothC$rothC);
> class(r1) # class is known by now
[1] "Rcpp_rothC"
attr(,"package")
[1] ".GlobalEnv"
> plot(r1) # "plot" is dispatched correctly
> showMethods("plot")
Function: plot (package graphics) # now "plot" from graphics shows up
x="ANY", y="ANY" # why was it missing before?
x="Rcpp_rothC", y="missing"
######### FILE rcpp_rothC_module.h #########
class rothC { ... }
######### FILE rcpp_rothC_module.cpp #########
using namespace Rcpp ;
RCPP_MODULE(rothC) {
class_<rothC>("rothC")
.constructor()
...
}
######### FILE rcpp_rothC.R #########
Rcpp_rothC.plot <- function(x,y,...) { ... }
########## FILE zzz.R ##########
.NAMESPACE <- environment()
rothC <- new( "Module" )
.onLoad <- function(libname, pkgname) {
unlockBinding( "rothC" , .NAMESPACE )
assign( "rothC", Module( "rothC" ), .NAMESPACE )
setMethod(f="plot",signature(x="Rcpp_rothC",y="missing"),definition=Rcpp_rothC.plot,where=.GlobalEnv);
lockBinding( "rothC", .NAMESPACE )
}
.onAttach <- function(libname, pkgname) {}
.onUnload <- function(libname, pkgname) { gc(); }
More information about the R-help
mailing list