[R] can I call user-created functions without source() ?

Duncan Murdoch murdoch at stats.uwo.ca
Mon Jun 19 13:22:48 CEST 2006


On 6/19/2006 5:36 AM, Rob Campbell wrote:
> Hi,
> 
> I have to R fairly recently from Matlab, where I have been used to
> organising my own custom functions into directories which are adding to
> the Matlab search path. This enables me to call them as I would one of
> Matlab's built-in functions. I have not found a way of doing the same
> thing in R. I have resorted to using source() on symlinks located in the
> current directory. Not very satisfactory.
> 
> I looked at the R homepage and considered making a "package" of my
> commonly used functions so that I can call them in one go:
> library(myFuncions, lib.loc="/path/to/library") Perhaps this is the only
> solution but the docs on the web make the process seem rather
> complicated--I only have a few script files I want to call! Surely
> there's a straightforward solution?
> 
> How have other people solved this problem? Perhaps someone has a simple
> "package skeleton" into which I can drop my scripts?

You could try the "package.skeleton" function.  It's not completely 
painless (you are still expected to edit some of the files, and to 
install the package), but it's not really so bad.  There are plans 
(already partially implemented in R-devel) to make it easier for the 
next release.  (So far I think the change is to require less editing on 
the default generated files.)

To answer the more general question: I generally use two styles of 
development.  For things where have long term usefulness, I go through 
the work above and create a package.  For more ephemeral things, I put 
them in a file, and in the file where I'm working on today's script, I 
put in source("blahblah.R") at the beginning.

You can also use a file named .Rprofile to contain commands to run; it 
is searched for in the current directory, then the user's home 
directory.  So you could put your source("blahblah.R") into .Rprofile if 
you want these functions to always be available.

Duncan Murdoch



More information about the R-help mailing list