Re: [R] Introduce a new function in a package?

From: Don MacQueen <>
Date: Thu 07 Apr 2005 - 00:55:43 EST

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.


At 4:09 PM +0100 4/6/05, Jan T. Kim wrote:
>On Wed, Apr 06, 2005 at 09:57:00AM -0400, Roger D. Peng wrote:
>> I think the usual way is to create an R package for yourself and load
>> it when you need it for whatever project.
>> -roger
>Alternatively, one can also write the function in question into one's
>~/.Rprofile; then, it's automatically available in all R sessions.
>To avoid confusion, make sure that you choose a unique name, i.e. one
>that isn't used by any package, if possible.
>This method should be used only for functions intended to provide some
>convenience in interactive sessions, code in scripts should not rely
>on functions being provided by ~/.Rprofile. For scripting, an R package
>is definitely preferred.
>Best regards, Jan
>> Luis Ridao Cruz wrote:
>> >R-help,
>> >
>> >Sometimes I define functions I wish to have in any R session.
>> >The obvious thing to do is copy-paste the code
>> >The thing is that sometimes I don't know where I have the function
>> >code.
>> >
>> >My question is if somehow I could define a function and "introduce" it
>> >(let's say 'base' package ) so that
>> >could be used anytime I run a different R project.
>> >
>> >Thank you in advance
>> >
>> >______________________________________________
>> > mailing list
>> >
>> >PLEASE do read the posting guide!
>> >
>> >
>> ______________________________________________
>> mailing list
>> PLEASE do read the posting guide!
> +- Jan T. Kim -------------------------------------------------------+
> | *NEW* email: |
> | *NEW* WWW: |
> *-----=< hierarchical systems are for files, not for humans >=-----*
> mailing list
>PLEASE do read the posting guide!

Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA

______________________________________________ mailing list
PLEASE do read the posting guide!
Received on Thu Apr 07 01:04:14 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:02 EST