[R] can I call user-created functions without source() ?
Michael H. Prager
Mike.Prager at noaa.gov
Mon Jun 19 17:38:38 CEST 2006
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")
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
>
--
Michael Prager, Ph.D.
Southeast Fisheries Science Center
NOAA Center for Coastal Fisheries and Habitat Research
Beaufort, North Carolina 28516
** Opinions expressed are personal, not official. No
** official endorsement of any product is made or implied.
More information about the R-help
mailing list