[Rcpp-devel] Adding methods using setMethod ?

notyour business pascal.ggl at gmail.com
Wed Feb 2 19:08:37 CET 2011


 Dear all,

I am re-posting this question to Rcpp-devel on suggestion on the R-help
list. Sorry for this cross-posting.

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 and works well. I am now trying to
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.

It has been suggested that the Rcpp class might have to be encapsulated into
a second S4 class (
http://r.789695.n4.nabble.com/Problem-with-quot-setMethod-quot-in-R-package-tt3238061.html),
but this would be a bit unfortunate since all C++ - fields and methods would
have to be "passed through" via extra code. Or did I miss something?

I'd appreciate your advice before re-organising this code.

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(); }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20110202/621d32b9/attachment.htm>


More information about the Rcpp-devel mailing list