Skip to content
Prev 9980 / 63424 Next

Packages, generics, S3 and S4

"Heywood, Giles" wrote:
(Frame 1 is an S-Plus concept; presumably you mean the global
environment, the first element in the search path.)

It seems likely you are not using the "saved image" option in the
INSTALL command, which you should use if your package has S4 classes or
methods.

The simple solution is to have a file called "install.R" in your main
package directory (not in the R/ subdirectory).  For most packages, the
file can be empty.  See the online documentation for INSTALL (but ignore
the comment about having require(methods) in the install.R file; this is
out of date since the methods package is normally included by default
now).

Standard calls to setClass, setMethod, etc. will then save the
definitions in an image file that is loaded when the package is
attached.  You need this, otherwise the definitions (currently) go to
the global environment.  That part will be fixed, but you want to use a
saved image anyway, since otherwise the call to library() for the
package will be much slower.
Generally, an S3 "generic" function (i.e., a function that calls
UseMethod()) is not much different from an ordinary function with no S3
methods, so far as defining S4 methods is concerned.

Either way

  setGeneric("plot")

creates a new generic function and makes the current function the
default method.  A call to setMethod() where there is no current S4
generic function for this name does the setGeneric call for you.

The call to setGeneric with a `def' argument is usually needed only when
there is no function of the same name and/or you do not want an existing
function to be called as the default.

John