<div class="moz-text-flowed" style="font-family: -moz-fixed; font-size: 12px;" lang="x-western">Dear all,
<br>
<br>I am re-posting this question to Rcpp-devel on suggestion on the R-help
list. Sorry for this cross-posting.
<br>
<br>I experience a problem in implementing a S4 class in a package and would
appreciate your help in this matter.
<br>
<br>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""...
<br>
<br>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.
<br>
<br>It has been suggested that the Rcpp class might have to be encapsulated
into a second S4 class
(<a class="moz-txt-link-freetext" href="http://r.789695.n4.nabble.com/Problem-with-quot-setMethod-quot-in-R-package-tt3238061.html">http://r.789695.n4.nabble.com/Problem-with-quot-setMethod-quot-in-R-package-tt3238061.html</a>),
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?
<br>
<br>I'd appreciate your advice before re-organising this code.
<br>
<br>Pascal
<br>
<br>(The error message and the relevant parts of the package files are
pasted below, together with my related questions as "comments".)
<br>
<br>
<br><blockquote type="cite" style="color: rgb(0, 0, 0);">showMethods("plot")
<br></blockquote>Function "plot":
<br> <not a generic function> # I am surprised about this --
<br> # why isn't plot a generic function?
<br>
<br><blockquote type="cite" style="color: rgb(0, 0, 0);">library("RrothC2")
<br></blockquote>Loading required package: Rcpp
<br>Loading required package: pascal
<br>Creating a new generic function for "plot" in ".GlobalEnv"
<br>in method for ‘plot’ with signature ‘x="Rcpp_rothC",y="missing"’: no
definition for class "Rcpp_rothC"
<br> # class seems not to be known
<br> # at this stage.
<br> # but where should I put setMethod()?
<br><blockquote type="cite" style="color: rgb(0, 0, 0);">r1 <- new(rothC$rothC);
<br>class(r1) # class is known by now
<br></blockquote>[1] "Rcpp_rothC"
<br>attr(,"package")
<br>[1] ".GlobalEnv"
<br><blockquote type="cite" style="color: rgb(0, 0, 0);">plot(r1) # "plot" is dispatched correctly
<br></blockquote>
<br><blockquote type="cite" style="color: rgb(0, 0, 0);">showMethods("plot")
<br></blockquote>Function: plot (package graphics) # now "plot" from graphics shows up
<br>x="ANY", y="ANY" # why was it missing before?
<br>x="Rcpp_rothC", y="missing"
<br>
<br>
<br>######### FILE rcpp_rothC_module.h #########
<br>
<br>class rothC { ... }
<br>
<br>######### FILE rcpp_rothC_module.cpp #########
<br>
<br>using namespace Rcpp ;
<br>
<br>RCPP_MODULE(rothC) {
<br> class_<rothC>("rothC")
<br> .constructor()
<br> ...
<br>}
<br>
<br>######### FILE rcpp_rothC.R #########
<br>
<br>Rcpp_rothC.plot <- function(x,y,...) { ... }
<br>
<br>########## FILE zzz.R ##########
<br>
<br>.NAMESPACE <- environment()
<br>
<br>rothC <- new( "Module" )
<br>
<br>.onLoad <- function(libname, pkgname) {
<br> unlockBinding( "rothC" , .NAMESPACE )
<br> assign( "rothC", Module( "rothC" ), .NAMESPACE )
<br>
<br>setMethod(f="plot",signature(x="Rcpp_rothC",y="missing"),definition=Rcpp_rothC.plot,where=.GlobalEnv);
<br> lockBinding( "rothC", .NAMESPACE )
<br>}
<br>
<br>.onAttach <- function(libname, pkgname) {}
<br>
<br>.onUnload <- function(libname, pkgname) { gc(); }
<br>
<br></div>