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

Joerg van den Hoff j.van_den_hoff at fz-rossendorf.de
Mon Jun 19 14:19:36 CEST 2006

Duncan Murdoch wrote:
> On 6/19/2006 6:25 AM, (Ted Harding) wrote:
>> On 19-Jun-06 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?
>>> Thanks,
>>> Rob
>> There are pros and cons to this, but on the whole I sympathise
>> with you (having pre-R been a heavy matlab/octave user myself).
>> Unfortunately (from this perspective) R does not seem to have
>> an automatic "load-on-demand" facility similar to what happens
>> in matlab (i.e. you call a function by name, and R would search
>> for it in whatever the current search-path is, and load its
>> definition plus what else it depends on).
>> I have a few definitions which I want in every R session, so
>> I have put these in my ".Rprofile". But this is loaded from
>> my home directory, not from the directory where I was when I
>> started R, so it is the same every time. 
> Which version of R are you using?  This is not the current documented 
> behaviour.  It looks in the current directory first, and only in your 
> home directory if that fails.
> Duncan Murdoch
> Again, one of the
>> conveniences of the matlab/octave approach is that you can
>> have a different sub-directory for each project, so if you
>> start work in a particular one then you have access to any
>> special definitions for that project, and not to others.
>> I'm no expert on this aspect of R, but I suspect that the way
>> start-up is organised in R does not fit well with the other
>> kind of approach. I stand to be corrected, of course ...
>> And others may well have formulated their own neat work-rounds,
>> so we wait eagerly to hear about these!
>> Best wishes,
>> Ted.

using `package.skeleton' (as already mentioned) for the package dir 
layout and `prompt' for generating templates of the needed manpages is 
not so bad (once you get used to it), if the things you have in mind are 
at least of some long(er) term value (for you): at least you are forced 
(sort of) to document your software...

for short term usage of some specialized functions I have added some 
lines to the `.Rprofile' in my home(!) directory as follows (probably 
there are smarter solutions, but at least it works):

#source some temporary useful functions:
fl <- dir(path='~/rfiles/current',patt='.*\\.R$',full.names=TRUE)
for (i in fl) {cat(paste('source("',i,'")\n',sep="")); source(i)}

here, I have put all the temporary stuff in a single dedicated dir 
`~/rfiles/current', but of course you can use several dirs in this way. 
all files in this dir with names ending in `.R' are sourced on startup 
of R. this roughly works like one of the directories on MATLAB's search 
path: every function definition in this directory is  'understood' by R 
(but everything is loaded into the workspace on startup, no matter, 
whether you really need it in the end: no real `load on demand'). one 
important difference, though: this is only sensible for function 
definitions, not scripts ('executable programms' (which would directly 
be executed on R startup, otherwise).
and, contrary to matlab/octave, this is not dynamic: everything is read 
in at startup, later modifications to the directories are not recognized 
without explicitely sourcing the files again.

if you in addition you want to load definitions from the startup 
directory where you launch R (your project dir), the above could be 
modified to:

#source some temporary useful functions from startup dir:
fl <- dir(path=getwd(),patt='.*\\.R$',full.names=TRUE)
for (i in fl) {cat(paste('source("',i,'")\n',sep="")); source(i)}

in this way you at least don't need a separate `.Rprofile' in each 
project dir.


More information about the R-help mailing list