[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 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [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, 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. 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) [EMAIL PROTECTED] Fax-to-email: +44 (0)870 094 0861 Date: 19-Jun-06 Time: 11:25:15 -- XFMail -- __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [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 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] can I call user-created functions without source() ?
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. E-Mail: (Ted Harding) [EMAIL PROTECTED] Fax-to-email: +44 (0)870 094 0861 Date: 19-Jun-06 Time: 11:25:15 -- XFMail -- __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] can I call user-created functions without source() ?
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)} rm(i,fl) 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)} rm(i,fl) in this way you at least don't need a separate `.Rprofile' in each project dir. joerg __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] can I call user-created functions without source() ?
Just a few comments below on alternative ways to do the same things: On 6/19/2006 8:19 AM, Joerg van den Hoff wrote: 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)} rm(i,fl) Another way to do this without worrying about overwriting some existing variables is local({ fl - ... for (i in fl) ... }) No need to remove fl and i at the end; they were created in a temporary environment, which was deleted at the end. 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'). It's possible to have load on demand in R, and this is used in packages. It's probably not worth the trouble to use it unless you're using a package. 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. There isn't really any reasonable way around this. I suppose some hook could be created to automatically read the file if the time stamp changes, but that's not really the R way of doing things: generally in R active things are in the workspace, not on disk. A good way to work is prepare things on disk, then when they are ready, explicitly import them into R. 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)} rm(i,fl) in this way you at least don't need a separate `.Rprofile' in each project dir. Another alternative if you want something special in the project is to create a .Rprofile file there, and put source(~/.Rprofile) into it, so both the local changes and the general ones get loaded. Duncan Murdoch joerg __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] can I call user-created functions without source() ?
Duncan Murdoch wrote: Just a few comments below on alternative ways to do the same things: On 6/19/2006 8:19 AM, Joerg van den Hoff wrote: 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)} rm(i,fl) Another way to do this without worrying about overwriting some existing variables is local({ fl - ... for (i in fl) ... }) No need to remove fl and i at the end; they were created in a temporary environment, which was deleted at the end. sure, that's better (just one more case, where I did'nt know of the existence of a certain function). but what is the difference (with regards to scope) of `i' or `fl' and the functions defined via sourcing? are'nt both objects defined within `local'? why _are_ the functions visible in the workspace? probably I again don't understand the `eval'/environment intricacies. 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'). It's possible to have load on demand in R, and this is used in packages. It's probably not worth the trouble to use it unless you're using a package. 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. There isn't really any reasonable way around this. I suppose some hook could be created to automatically read the file if the time stamp changes, but that's not really the R way of doing things: generally in R active things are in the workspace, not on disk. A good way to work is prepare things on disk, then when they are ready, explicitly import them into R. 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)} rm(i,fl) in this way you at least don't need a separate `.Rprofile' in each project dir. Another alternative if you want something special in the project is to create a .Rprofile file there, and put source(~/.Rprofile) into it, so both the local changes and the general ones get loaded. Duncan Murdoch joerg __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] can I call user-created functions without source() ?
On 6/19/2006 10:19 AM, Joerg van den Hoff wrote: Duncan Murdoch wrote: Just a few comments below on alternative ways to do the same things: On 6/19/2006 8:19 AM, Joerg van den Hoff wrote: 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)} rm(i,fl) Another way to do this without worrying about overwriting some existing variables is local({ fl - ... for (i in fl) ... }) No need to remove fl and i at the end; they were created in a temporary environment, which was deleted at the end. sure, that's better (just one more case, where I did'nt know of the existence of a certain function). but what is the difference (with regards to scope) of `i' or `fl' and the functions defined via sourcing? are'nt both objects defined within `local'? why _are_ the functions visible in the workspace? probably I again don't understand the `eval'/environment intricacies. Whoops, sorry. Yes, you'd need to be careful to assign them into globalenv(). Those ...'s weren't so simple. Duncan Murdoch __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] can I call user-created functions without source() ?
Duncan Murdoch wrote: On 6/19/2006 10:19 AM, Joerg van den Hoff wrote: Duncan Murdoch wrote: Just a few comments below on alternative ways to do the same things: On 6/19/2006 8:19 AM, Joerg van den Hoff wrote: 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)} rm(i,fl) Another way to do this without worrying about overwriting some existing variables is local({ fl - ... for (i in fl) ... }) No need to remove fl and i at the end; they were created in a temporary environment, which was deleted at the end. sure, that's better (just one more case, where I did'nt know of the existence of a certain function). but what is the difference (with regards to scope) of `i' or `fl' and the functions defined via sourcing? are'nt both objects defined within `local'? why _are_ the functions visible in the workspace? probably I again don't understand the `eval'/environment intricacies. Whoops, sorry. Yes, you'd need to be careful to assign them into globalenv(). Those ...'s weren't so simple. no, no, you _were_ right (I tested it), but I did'nt understand why it works. I found the answer only now in the `source' manpage: there is an argument `local = FALSE' which by default enforces assignment into globalenv(). so the `...' remain unaltered :-). sorry for the confusion. joerg PS: and as a follow up on the recent 'setwd vs. cd' thread: as I noted in passing `source' has a further argument `chdir' to change the directory prior to sourcing, which is neither the usual name (using `cd') nor the R-way (using `setwd'), but rather identical to the corresponding native matlab/octave command (both of these packages have for quite some time changed to accepting `cd' as well). of course, the `source' arguments have nothing to do with the commands but consistency would'nt harm either :-). not important, but a bit funny. Duncan Murdoch __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [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. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] can I call user-created functions without source() ?
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 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] can I call user-created functions without source() ?
I'd like to echo Duncan's comment about creating 'man' pages for functions. I find myself writing man pages for functions I may never include in an R package, just because it helps me think through what I'm trying to do. I think I get better code faster. It's like preparing a map before starting on a journey through unknown territory: I may think I know the destination and more or less every step of the way. However, if it's very far, it helps to make a plan first; then when I encounter something I hadn't anticipated, it makes it much easier to figure out what to do without sabotaging something else. Hope this helps. Spencer Graves Duncan Murdoch wrote: 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 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] can I call user-created functions without source() ?
Indeed, some folk say that the documentation should be written before the code... Sean On 19/06/06, Duncan Murdoch [EMAIL PROTECTED] wrote: 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 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] can I call user-created functions without source() ?
on 6/19/2006 1:32 PM Spencer Graves said the following: I'd like to echo Duncan's comment about creating 'man' pages for functions. I find myself writing man pages for functions I may never include in an R package, just because it helps me think through what I'm trying to do. I think I get better code faster. It's like preparing a map before starting on a journey through unknown territory: I may think I know the destination and more or less every step of the way. However, if it's very far, it helps to make a plan first; then when I encounter something I hadn't anticipated, it makes it much easier to figure out what to do without sabotaging something else. Yes, I agree on the value of planning and documentation. I hope this year to have the time to learn the package-generation system, which on first impression seems a bit trickier on Windows than on Unix etc. Last but not least, thanks for the tips on load() vs. attach(). ...Mike -- 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. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html