Skip to content
Prev 67118 / 398506 Next

Introduce a new function in a package?

Expressions in .Rprofile are executed *before* any previously saved 
global environment is loaded (i.e., before the .RData file in the 
current working directory is loaded, causing the message " 
[Previously saved workspace restored]" to a appear).

If you define a function in .Rprofile, and then later answer "yes" to 
the "Save workspace image?" question when you quit R, the function 
will exist in the saved workspace.

When you next start R, the version that comes in from .Rprofile will 
be replaced by the version in the saved workspace -- because the 
saved workspace is loaded after .Rprofile is executed.

This means that if you decide to change the function in .Rprofile, 
your changes will immediately be lost when the previously saved 
workspace is loaded, since that has the previous version.

So defining personal utility functions in .Rprofile is not very 
effective. Much, much, better to create a package, and then require() 
that package in .Rprofile. And since creating a package is really 
very easy, I strongly recommend that option.

Saving the functions in an image file and then attaching it is fine, 
but less convenient, in my opinion, since you have to keep track of 
where it is in the file system.

-Don
At 4:09 PM +0100 4/6/05, Jan T. Kim wrote: