From ripley at stats.ox.ac.uk  Thu Nov 12 11:54:46 2009
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Thu, 12 Nov 2009 10:54:46 +0000 (GMT)
Subject: [Rd] S4 objects in the data directory
In-Reply-To: <4AFBDA57.9000905@cam.ac.uk>
References: <4AFBDA57.9000905@cam.ac.uk>
Message-ID: <alpine.LFD.2.00.0911121031370.21185@toucan.stats.ox.ac.uk>

On Thu, 12 Nov 2009, Robin Hankin wrote:

> Hi
>
> [R-2.10.0; suse linux]
>
> I am having difficulty creating S4 objects in the data directory of a 
> package. I want
> to create a bunch of simple S4 objects for use in the examples section of the 
> Rd files.
>
> It says in R-exts that:
>
> " R code should be ?self-sufficient? and not make use of extra functionality 
> provided by the package, so that the data file can also be used without 
> having to load the package"
>
> My current minimal self-contained example follows.
>
> le112:~% cat ./anRpackage/R/f.R
> setClass("foo", representation=representation(x="numeric"))
>
> le112:~% cat ./anRpackage/data/toy_foo.R
> "toy_foo" <- new("foo",x=rep(2,7))
>
> fails R CMD check (transcript below)
>
> It fails because the class isn't defined. I can
> add 'require(anRpackage)' to the toy_foo.R
> but I'm not sure if this is consistent with the above
> advice in R-exts (and it
> fails R CMD check anyway).

It isn't consistent.

> What is best practice for creating a package with
> "toy" S4 objects for use in the examples section
> of the Rd files?

Why not do this either in the examples section, or in a
make_data_for_examples() function in the package?

I see the data() mechanism as having been largely superseded by 
lazy-loading.  It was primarily a way to avoid having large datasets 
always loaded (and taking up memory and gc() time).  It is sometimes 
convenient to load a dataset from a package without loading that 
package: see the help for 'alias' and 'logLik' for example, but if you 
don't need that possibility (and it seems that in your case it is not 
available to you) you might as well have the object lazy-loaded in the 
package or accessed by an accessor function in the package.

>
>
> cheers
>
>
> Robin
>
>
>
>
>
>
>
>
> le112:~% R CMD check ./anRpackage
> * checking for working pdflatex ... OK
>
> [snip]
>
> * checking data for non-ASCII characters ... NOTE
> Error: "foo" is not a defined class
> Call sequence:
> 8: stop(gettextf("\"%s\" is not a defined class", Class), domain = NA)
> 7: getClass(Class, where = topenv(parent.frame()))
> 6: new("foo", x = rep(2, 7))
> 5: eval(expr, envir, enclos)
> 4: eval(i, envir)
> 3: sys.source(zfile, chdir = TRUE, envir = envir)
> 2: switch(ext, R = , r = {
> library("utils")
> sys.source(zfile, chdir = TRUE, envir = envir)
> }, RData = , rdata = , rda = load(zfile, envir = envir), TXT = ,
> txt = , tab = , tab.gz = , tab.bz2 = , tab.xz = , txt.gz = ,
> txt.bz2 = , txt.xz = assign(name, read.table(zfile, header = TRUE,
> as.is = FALSE), envir = envir), CSV = , csv = , csv.gz = ,
> csv.bz2 = , csv.xz = assign(name, read.table(zfile, header = TRUE,
> sep = ";", as.is = FALSE), envir = envir), found <- FALSE)
> 1: utils::data(list = f, package = character(0L), envir = dataEnv)
> Execution halted
> Portable packages use only ASCII characters in their datasets.
> * checking examples ...no parsed files found
> NONE
>
> WARNING: There were 2 warnings, see
> /home/rksh/anRpackage.Rcheck/00check.log
> for details
>
> le112:~%
>
>
>
> -- 
> Robin K. S. Hankin
> Uncertainty Analyst
> University of Cambridge
> 19 Silver Street
> Cambridge CB3 9EP
> 01223-764877
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


[Rd] S4 objects in the data directory
Prof Brian Ripley