Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

: Run code first time a package is installed or used

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.

like image 775
Joris Meys Avatar asked Oct 25 '10 21:10

Joris Meys


1 Answers

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.

like image 176
Dirk Eddelbuettel Avatar answered Sep 23 '22 08:09

Dirk Eddelbuettel