Skip to content

S4 objects in the data directory

2 messages · robin hankin, Brian Ripley

#
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).

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


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:~%
#
On Thu, 12 Nov 2009, Robin Hankin wrote:

            
It isn't consistent.
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.