From hb at maths.lth.se  Wed Feb 16 15:05:44 2005
From: hb at maths.lth.se (Henrik Bengtsson)
Date: Wed Feb 16 15:11:26 2005
Subject: [Rd] Depends, require(), autoload() and "side effects"?
Message-ID: <002101c51430$9c7ab3c0$57f1ba51@hblaptop>

Hi.

If your package require another package in order to define its methods (but
afterwards in is not needed), how should one specify this? Is it correct to
assume that "Depends" will assure that all packages are loaded (attached?
what is the right word) *before* the methods in the current package are
defined? Basically I use my own setMethod() called setMethodS3() that is
defined in package R.oo.

In pre R v2.0.0 this could be done adding a require() at the beginning of
the package source, but since R v2.0.0 this should be avoided according to
R-exts [R v2.1.0 devel] (Section 1.1.4; "The R code files should only create
R objects and not call functions with side effects such as require and
options.").

DESCRIPTION/Depends: In R-ext Section 1.1.1 it says "The R INSTALL
facilities check if the version of R used is recent enough for the package
being installed, and the list of packages which is specified [in Depends]
will be attached (after checking version dependencies) before the current
package, both when library is called and when saving an image of the
package's code or preparing for lazy-loading." and further done "The general
rules are: ... Packages that need to be attached to successfully load the
package using library(pkgname) must be listed in the Depends field."

I use Depends, which works (installs from source and loads) on R v2.0.1
patch and R v2.1.0 devel on WinXP Pro, but not on R v2.0.1 on Sun Solaris.
Here is how I do it:

In my R.oo package I define setMethodS3() to simplify creation of S3 methods
and generic functions. Other packages of mine make use of this method (and
some other methods of R.oo such as appendVarArgs() as seen below) when
defining their methods. 

Consider a package R.matlab that depends on R.oo for the above reason. I do
not use namespaces.  To attach R.oo to load R.matlab, I add R.oo in the
Depends field of DESCRIPTION as follows:

Package: R.matlab
Version: 1.0
Date: 2005-02-15
Title: Matlab connectivity and read and write of MAT files
Author: Henrik Bengtsson <henrikb@braju.com>
Maintainer: Henrik Bengtsson <henrikb@braju.com>
Depends: R.oo
Description: This package provides methods to read and write MAT files.  It
also makes it possible to communicate (evaluate code, send and retrieve
objects etc.) with Matlab v6 or higher running locally or on a remote host.
The auxillary Java class provides static methods to read and write Java data
types.
License: GPL version 2 or newer
URL: http://www.braju.com/R/
LazyLoad: TRUE 

I then build a source package using R v2.1.0 devel (on WinXP Pro).
Installation from source and package loading works fine on WinXP Pro with R
v2.0.1 patched and R v2.1.0 devel.  On a Solaris with R v2.0.1 (standard), I
get: 

% R CMD INSTALL R.matlab_1.0.tar.gz
* Installing *source* package 'R.matlab' ...
** R
** inst
** preparing package for lazy loading
Error in eval(expr, envir, enclos) : couldn't find function "appendVarArgs"
Execution halted
ERROR: lazy loading failed for package 'R.matlab'
** Removing '/usr/matstat/hb/R/R_LIBS/sunos/library/R.matlab'
** Restoring previous '/usr/matstat/hb/R/R_LIBS/sunos/library/R.matlab'

appendVarArgs() is defined in R.oo. 

I tried to add a require(R.oo) (in 000.R) and it does work perfectly. So do
also autoload("appendVarArgs", package="R.oo") etc. Is autoload() ok to use,
or does it use side effects? Is even require(R.oo) ok to use for the purpose
to *load* the package?

Best wishes

Henrik Bengtsson


[Rd] Depends, require(), autoload() and "side effects"?
Henrik Bengtsson