[Rd] particulars of importing/loading libraries

Sklyar, Oleg (London) osklyar at maninvestments.com
Tue Jan 13 17:00:53 CET 2009


Dear List:

Sorry for posting maybe a trivial question, but I have a basic
understanding problem. If I have say pack1 and pack2, two R packages,
and pack2 depends on and imports pack1 fully (as in the code below), is
there a way to make all the functionality of pack1 available for the
global and other environments (not only for the functions called from
withing pack2) by loading pack2 only? I thought if pack2 depends on and
imports pack1 and essentially reexports everything, one should get the
full functionality simply by loading pack2. This does not seem to be the
case or I am missing something trivial in my NAMESPACE/DESCRIPTION
files?

If this is documented in Writing R Extensions, I would be thankful for a
page number and maybe a quick fix in my example below as so far I have
not been able to find a clear explanation.

The problem can be illustrated by the following simple example (this is
a simple code for 2 packages, pack1 and pack2; plus an example).

Thank you for your replies.

Dr Oleg Sklyar
Research Technologist
AHL / Man Investments Ltd
+44 (0)20 7144 3107
osklyar at maninvestments.com

--- pack1: DESCRIPTION ------
Package: pack1
Version: 0.0.1
Date: 12 Jan 2009
Title: pack1 to test S3/S4 methods compatibility
Author:  Oleg Sklyar
Depends: R (>= 2.7.1), methods
Maintainer: Oleg Sklyar <osklyar at maninvestments.com>
Description: pack1
LazyLoad: yes
License: Proprietary
URL: http://www.maninvestments.com
LazyLoad: no

--- pack1: NAMESPACE ------
import(methods)
exportPattern("^[^\\.]")
exportClasses(posixTime)
exportMethods(as.POSIXct)

--- pack1: posix.R ------
setClass("posixTime", "numeric")

setGeneric("as.POSIXct")
setMethod("as.POSIXct", signature(x="posixTime"),
    function(x, tz) {
        z = x at .Data
        attr(z,"class") = c("POSIXt", "POSIXct")
        attr(z,"tzone") = "UTC"
        z
    }
)

testPosixVal = new("posixTime", as.numeric(Sys.time()))

--- pack2: DESCRIPTION 
Package: pack2
Version: 0.0.1
Date: 12 Jan 2009
Title: pack2 to test S3/S4 methods compatibility
Author:  Oleg Sklyar
Depends: R (>= 2.7.1), methods
Maintainer: Oleg Sklyar <osklyar at maninvestments.com>
Description: pack2
LazyLoad: yes
License: Proprietary
URL: http://www.maninvestments.com
LazyLoad: no

--- pack2: NAMESPACE ------
import(pack1)
exportPattern("^[^\\.]")

--- pack2: posix.R ------
testPosix = function() {
    z = as.POSIXct(testPosixVal)
    print(z)
    print(class(z))
    z
}

------ test code to run from global env, showing problems -------
require(pack2)

## use as.POSIXct imported into pack2 from pack1 to do the conversion in
the fun
testPosix()
#~ [1] "2009-01-13 15:29:50 UTC"
#~ [1] "POSIXt"  "POSIXct"
#~ [1] "2009-01-13 15:29:50 UTC"

## now try using it directly from the global env (pack1 was not
explicitly loaded)
as.POSIXct(pack1::testPosixVal)
#~ Error in as.POSIXct.default(pack1::testPosixVal) : 
#~  do not know how to convert 'pack1::testPosixVal' to class "POSIXlt"

## now require pack1 explicitly and try again
require(pack1)
as.POSIXct(pack1::testPosixVal)
#~ [1] "2009-01-13 15:29:50 UTC"


**********************************************************************
Please consider the environment before printing this email or its attachments.
The contents of this email are for the named addressees ...{{dropped:19}}



More information about the R-devel mailing list