tags:

views:

59

answers:

2

Hi All,

I am busy writing a package for a customer with little knowledge about R. Given their complex data structure, I need to set up a "data base" within R containing tons of information obtained from a set of spreadsheets they get from another company. As they can't install SQL or so on their computers (ICT has some power control issues...), I've written an emulation in R, based on a specific directory structure. Now I want to run this automatically, but only the first time the package is loaded. Something like .First.lib, but then .VeryFirst.

Any idea on how to load a piece of code the first time a package is loaded? I couldn't really find it anywhere in the manuals, so all pointers are welcome.

A: 

It's in the manuals.

Basically you have two code paths:

  1. packages without a NAMESPACE can use a function .First.lib(), typically from R/zzz.R

  2. packages with a NAMESPACE can use a function .onLoad(), also often from R/zzz.R.

I have used this for tricks like having a package update itself (!!) when loaded. That required not using a NAMESPACE and running utils::update.packages() before actually loading binary code.

Dirk Eddelbuettel
@Dirk: Thx for the answer, but don't these functions load the code every time you load the package, or did I get the help files completely wrong? I'd like to call the code only the first time the package is loaded.
Joris Meys
Sure, so you use `file.exists()` and friends to check whether the structure exists and skip creating it the second, third, ... time around. But you asked where to create it on startup, and I tried to answer that question.
Dirk Eddelbuettel
Is it possible for a package to update itself on windows? Or does the weird file locking cause problems?
hadley
Yes, I got it working on Windows despite the awful locking by not using a NAMESPACE which delays the dynamic loading. I then call `library.dynam()` after the call to `update.packages()`. That worked -- but it is a hack (as I'd rather keep a NAMESPACE).
Dirk Eddelbuettel
@Dirk: then we understood eachother wrong. I meant the _first_ startup. I'm well acquainted with onLoad and .First.lib
Joris Meys
A: 

What about specifying a path and nomenclature for them to put the spreadsheets into. You can recurse the directory for updates and files. I do this for a similar problem.

2010 08.xls 2010 09.xls

You could use a readline() to prompt for new data ranges as well making it so they only have to type the new month. And if the directory ever changes, it's not difficult to teach someone how to setwd() or to update it yourself using a script that checks something on your personal/company server.

Are they actually loading R to run the package? Or is it all command line?

Brandon Bertelsen