[R] load data for mypkg-Ex.R

Jason Sinnwell Sinnwell.Jason at mayo.edu
Thu Mar 4 15:58:29 CET 2004


Thank you for your suggestion.  I think I have a good solution to my issues.  
And for reference I thought I would post what worked for me.  

I had to make a function called setupData() that gets the same argument in Splus 
and R.  I had to modify your suggestion a little because the sgml and Rd files 
were expecting different parameters.  This function runs exactly as data() in R 
and does nothing in Splus. 

setupData <- function(...) {
    result <- if(exists("is.R") && is.function(is.R) && is.R()) {
               data
             } else {
               function(...) invisible(NULL)
             }
    eval(result(...))
}

Then in all the help files which use a data set I have:
> setupData(my.data)
As wished for, and expected.

If I had more data sets to manage (maybe someday), I would probably mimic the 
clever use of delay() in the MASS library.  

> Date: Fri, 27 Feb 2004 10:20:50 -0800 (PST)
> From: Thomas Lumley <tlumley at u.washington.edu>
> To: Jason Sinnwell <Sinnwell.Jason at mayo.edu>
> cc: r-help at stat.math.ethz.ch
> Subject: Re: [R] load data for mypkg-Ex.R
> MIME-Version: 1.0
> 
> On Fri, 27 Feb 2004, Jason Sinnwell wrote:
> 
> > Using R 1.7.1 in Solaris
> >
> > I'm developing a package for both Splus and R, and I'm trying to use all the
> > same files for R and Splus, both function files and help files.  I have two
> > questions.
> >
> > 1) The file made by R CMD check to run .Rd-examples posts examples from 
files in
> > alphabetical order.  Is it okay/recommended/common-practice to set up all 
the
> > example data in the first two (alphabetically-sorted) examples and assume 
that
> > data exists for the rest of the examples?
> 
> No.  In fact, it is specifically disallowed.
> 
> > 2)  Since data() is not understood by Splus, I don't want to put a
> > 	     > data(example.data)
> > in the sgml file because then the Splus example would not run as data() 
doesn't
> > exist there.  Is there a spot I can make sure this data is loaded when 
running
> > the examples, but not to load the data every time you load the library, as 
it
> > would take up unnecessary space.  It is a ~~220 x 25 data.frame, is that 
enough
> > size to worry about this?
> 
> I wouldn't think it was big enough to worry seriously about
> 
> > I'm considering using the NAMESPACE or .First.lib() within zzz.R but that 
would
> > load the data every time the library is loaded.  Also considering something
> > like:
> > >if (<check for R using is.R()>)
> > >  data(example.data)
> > > <run example>
> >
> > In the example but that would create confusion for users.
> 
> You could define a function
> 
> if (is.R())
>   setupData<-data
> else
>   setupData<-function(...) invisible(NULL)
> 
> and then use setupData() instead of data(), or you could look at what MASS
> does using delay() to autoload data as needed.
> 
> 
> 	-thomas

+--------------------------+
|Jason P. Sinnwell, M.S.   |
|Mayo Clinic, Rochester    |
|Health Sciences Research  |
|Division of Biostatistics |
|507.284.3270              |
+--------------------------+




More information about the R-help mailing list