[R] Writing packages with `methods' package

Roger Peng rpeng at stat.ucla.edu
Fri Jan 3 08:26:03 CET 2003


Hello, thanks for the tip.  I tried creating the `install.R' file in the
root level of the package directory and the build/installation went fine.
However, I when I load the package after doing this type of build, all my
methods work fine except of the method I define for "[".  When I try to
use the "[" on my own object, it seems to be resorting to the default "["
method (if I understand that correctly) rather than the one I defined.  Is
this a problem of where my method gets defined in the search path?  I
figured that since my method is higher up in the search path, it should
get called before the one in the `base' package.

If it helps, here are the class and method definitions.  What I find
strange is that all of my other methods get called correctly except this
one.

setClass("gpc.poly",
         representation(pts = "list"),
         prototype(pts = NULL)
         )

setMethod("[", signature(x = "gpc.poly", j = "missing"),
          function(x, i, ..., drop = FALSE) {
              x at pts <- x at pts[i]
              x
          })

Thanks,

-roger
_______________________________
UCLA Department of Statistics
rpeng at stat.ucla.edu
http://www.stat.ucla.edu/~rpeng

On Tue, 31 Dec 2002, John Chambers wrote:

> The problem here is that the standard R way of attaching a package is to
> source in the R code; when the code contains setMethod() calls and the
> like, the result is to store the resulting definitions in the global
> environment.
> 
> When you're writing packages using methods, you essentially always want
> to use an alternative installation for the package, which creates a
> binary image when R INSTALL runs.   This image is loaded when the
> package is attached by calling library(), and the objects for methods,
> etc. are in the package database,as you would like.
> 
> There's a paragraph in the online documentation for INSTALL that
> describes what to do.  (if you want a quick fix, just touch an empty
> file install.R in the package's top level directory.)
> 
> When you run INSTALL, you should see a line:
> 
> ** save image
> 
> in the printout.  And no files generated in the global environment from
> calling library()
> 
> Regards,
>  John Chambers
> 
> Roger Peng wrote:
> > 
> > I'm trying to write a package which uses classes/methods as defined in the
> > `methods' package.  I have a single .R file which defines the class and
> > various methods for that class.  At the top of the file I have
> > 
> > require(methods)
> > 
> > and then
> > 
> > setClass("myclass", ...)
> > setGeneric("intersect")
> > setMethod("intersect", "myclass", function(x,y) ...)
> > 
> > I noticed that when I build the package and subsequently load it via
> > library(), the methods show up in the global workspace, which is not quite
> > what I wanted.
> > 
> > In general, is there any documentation on building packages with the
> > `methods' package (i.e. is it any different from building packages without
> > `methods'?) or perhaps an R-help thread I should look for?
> > 
> > In short, how should I setup my package so that my methods do not show up
> > in the global workspace?
> > 
> > Thanks,
> > 
> > -roger
> > _______________________________
> > UCLA Department of Statistics
> > rpeng at stat.ucla.edu
> > http://www.stat.ucla.edu/~rpeng
> > 
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > http://www.stat.math.ethz.ch/mailman/listinfo/r-help
> 
> -- 
> John M. Chambers                  jmc at bell-labs.com
> Bell Labs, Lucent Technologies    office: (908)582-2681
> 700 Mountain Avenue, Room 2C-282  fax:    (908)582-3340
> Murray Hill, NJ  07974            web: http://www.cs.bell-labs.com/~jmc
>




More information about the R-help mailing list