Skip to content

loading a package .Rda file at package load time

3 messages · Charlie Sharpsteen, Rajarshi Guha

#
Hi, I have seen the answer to this sometime before but I just can't find it
again - pointers appreciated.

I have a package that contains some data.frames saved as .Rda files in the
data/ directory. When the package is loaded I would like to have them be
available in the workspace (without the user having to explicitly load them
using data(...)).

If my package does not use a NAMESPACE, I can place data(varName) in
.First.lib. However if I use NAMESPACES then the above line will fail when
placed in .onLoad. I was thinking of doing something such as
load'filename.Rda') but that would require me to know the path to the
package in a platform independent way.

Can anybody suggest how I can acheive this?

Thanks,

-- 
Rajarshi Guha

</quote

You mentioned that you used something similar to:

.First.lib <- function( libname, pkgname ){

  data( varName )

}

And it didn't work when you attached a NAMESPACE to the package. If your
.Rda files are stored in the data directory of the package, have you tried
using:

.First.lib <- function( libname, pkgname ){

  data( varName, package = pkgname )

}


If that doesn't work, then system.file() may be used to explicity recover
the location of your package data files:

.First.lib <- function( libname, pkgname ){

  data( system.file( 'data/myDataFile.Rda', package = pkgname )

}

Good luck!

-Charlie

-----
Charlie Sharpsteen
Undergraduate
Environmental Resources Engineering
Humboldt State University