Rob Campbell
2006-Jun-19 09:36 UTC
[R] can I call user-created functions without source() ?
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 -- Rob Campbell - Research Student Autistic Bacteriophage Research Group www.autisticBacteriophage.notlong.com Oxford www.robertcampbell.co.uk
(Ted Harding)
2006-Jun-19 10:25 UTC
[R] can I call user-created functions without source() ?
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, > > RobThere 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. 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. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 19-Jun-06 Time: 11:25:15 ------------------------------ XFMail ------------------------------
Duncan Murdoch
2006-Jun-19 11:22 UTC
[R] can I call user-created functions without source() ?
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
Michael H. Prager
2006-Jun-19 15:38 UTC
[R] can I call user-created functions without source() ?
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.