<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 &quot;library(...)&quot;), 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 &quot;creating new generic function for &quot;plot&quot; in 
&quot;.GlobalEnv&quot;, as well as &quot;no definition for class &quot;Rcpp_rothC&quot;&quot;...
<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 &quot;passed through&quot; via extra code. Or did I miss something?
<br>
<br>I&#39;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 &quot;comments&quot;.)
<br>
<br>
<br><blockquote type="cite" style="color: rgb(0, 0, 0);">showMethods(&quot;plot&quot;)
<br></blockquote>Function &quot;plot&quot;:
<br> &lt;not a generic function&gt;      # I am surprised about this --
<br>                               # why isn&#39;t plot a generic function?
<br>
<br><blockquote type="cite" style="color: rgb(0, 0, 0);">library(&quot;RrothC2&quot;)
<br></blockquote>Loading required package: Rcpp
<br>Loading required package: pascal
<br>Creating a new generic function for &quot;plot&quot; in &quot;.GlobalEnv&quot;
<br>in method for ‘plot’ with signature ‘x=&quot;Rcpp_rothC&quot;,y=&quot;missing&quot;’: no 
definition for class &quot;Rcpp_rothC&quot;
<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 &lt;- new(rothC$rothC);
<br>class(r1)                    # class is known by now
<br></blockquote>[1] &quot;Rcpp_rothC&quot;
<br>attr(,&quot;package&quot;)
<br>[1] &quot;.GlobalEnv&quot;
<br><blockquote type="cite" style="color: rgb(0, 0, 0);">plot(r1)                     # &quot;plot&quot; is dispatched correctly
<br></blockquote>
<br><blockquote type="cite" style="color: rgb(0, 0, 0);">showMethods(&quot;plot&quot;)
<br></blockquote>Function: plot (package graphics) # now &quot;plot&quot; from graphics shows up
<br>x=&quot;ANY&quot;, y=&quot;ANY&quot;                  # why was it missing before?
<br>x=&quot;Rcpp_rothC&quot;, y=&quot;missing&quot;
<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_&lt;rothC&gt;(&quot;rothC&quot;)
<br>    .constructor()
<br>   ...
<br>}
<br>
<br>######### FILE rcpp_rothC.R #########
<br>
<br>Rcpp_rothC.plot &lt;- function(x,y,...) { ... }
<br>
<br>########## FILE zzz.R ##########
<br>
<br>.NAMESPACE &lt;- environment()
<br>
<br>rothC &lt;- new( &quot;Module&quot; )
<br>
<br>.onLoad &lt;- function(libname, pkgname) {
<br>  unlockBinding( &quot;rothC&quot; , .NAMESPACE )
<br>  assign( &quot;rothC&quot;,  Module( &quot;rothC&quot; ), .NAMESPACE )
<br>
<br>setMethod(f=&quot;plot&quot;,signature(x=&quot;Rcpp_rothC&quot;,y=&quot;missing&quot;),definition=Rcpp_rothC.plot,where=.GlobalEnv);
<br>  lockBinding( &quot;rothC&quot;, .NAMESPACE )
<br>}
<br>
<br>.onAttach &lt;- function(libname, pkgname) {}
<br>
<br>.onUnload &lt;- function(libname, pkgname) { gc(); }
<br>
<br></div>