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

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


On 6/19/2006 11:38 AM, Michael H. Prager wrote:
> Rob,
> 
> I accomplish what you ask by putting my functions into a dedicated 
> directory, starting R there and sourcing them, and loading the resulting 
> workspace in .Rprofile with this:
> 
> attach("d:/R/MHP/MHPmisc/.RData")

This seems like a nice approach.  Some comments:

1.  attach or load?

You could also use

  load("d:/R/MHP/MHPmisc/.RData")

One difference is that this would load the functions into the user's 
workspace, overwriting any existing functions of the same name, whereas 
attach() puts them on the search list.  If the user made changes to the 
functions, they would replace the loaded ones (for that session, not in 
the .RData file).

Another difference is that attach() is cumulative.  I.e. if you forget 
and load() again, the functions will just revert to their saved 
definitions, but if you forget and attach() again, you'll get another 
copy of the functions in memory.  Probably harmless, but potentially 
confusing if you think detach() is going to remove them.

I don't know which of attach() or load() you'd find to be preferable.

2.  If you use the package system, you will be encouraged to create man 
pages for your functions.  This is work, but I think it pays off in the 
end.  I often find that when I document a function I realize an error in 
the design, and I end up improving it.  It's also useful to have 
documentation for functions that you don't use every day, or when using 
functions written by someone else.

Duncan Murdoch

> 
> I find that procedure simpler than learning the package mechanism.  It 
> is easy to add new functions periodically.
> 
> Not long ago, I posted the R code I used to automate the process.  As 
> the archive seems unreachable right now (from here, anyway) and the code 
> is relatively short, I'll post it again:
> 
> ##########################################################
> ## 00make.r       MHP      Dec 2005
> ## This R script clears the current workspace, sources all R scripts
> ## found in the working directory, and then saves the workspace for
> ## use in other R sessions.
> 
> # Clear all existing objects in workspace:
> rm(list=ls())
> 
> # Make a list of all R source files in this directory:
> flist = list.files(path=".", pattern=".+\.r")
> 
> # Remove from the list all files containing the string "00":
> # Such files should be used for temporary funcs:
> flist2 = flist[-grep("00",flist)]
> 
> # Source the files:
> for (i in 1:length(flist2)) {
>    cat("Sourcing", flist2[i],"\n")
>    source(flist2[i])
>    }
> # Remove temporary objects:
> rm(i,flist,flist2)
> # Save workspace:
> save.image()
> # Write message to user:
> cat("\nNOTE: The workspace has been saved with all functions.\n",
>     "     When exiting R, please do NOT save again.\n")
> ls()
> ##########################################################
> 
> I run that script straight from the (Windows) command line with the 
> following shell script:
> 
> rterm.exe --no-restore --no-save < 00make.r > 00make.txt
> 
> You will probably need to modify that slightly to work under Unix/Linux.
> 
> Hope that helps.
> 
> ...Mike
> 
> 
> 
> on 6/19/2006 5:36 AM Rob Campbell said the following:
>> 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
>>   
>



More information about the R-help mailing list