Sergio Correia
2007-Oct-09 02:02 UTC
[R] How to create something between a script and a package
(Before starting: I'm a total R noob so please bear with me in case of any error or faux pas). Hi, For a small project, I'm writing a few simple R functions and calling them from python (using RPy). I'm sharing the code with a couple of friends, using a subversion server. Now, I want something like a package, to be able to share functions between ourselves. Having read a few package tutorials, I feel that this is not exactly what I want and I'm kinda lost regarding what alternatives are available. What I want could be summarized as this: - A plain text file (eg: myfunctions.R) - Can be located in other folders besides the default (easier to synchronize using subversion). Eg: myproject/rfunctions/myfunctions.R - Can be accessed from the R console (without having to load a specific workspace). Is there any solution that can provide this? Any suggestions would be greatly appreciated, Thanks, Sergio
Vladimir Eremeev
2007-Oct-09 07:49 UTC
[R] How to create something between a script and a package
Sergio Correia wrote:> > (Before starting: I'm a total R noob so please bear with me in case of > any error or faux pas). > > Hi, > > For a small project, I'm writing a few simple R functions and calling > them from python (using RPy). I'm sharing the code with a couple of > friends, using a subversion server. > > Now, I want something like a package, to be able to share functions > between ourselves. Having read a few package tutorials, I feel that > this is not exactly what I want and I'm kinda lost regarding what > alternatives are available. > > What I want could be summarized as this: > - A plain text file (eg: myfunctions.R) > - Can be located in other folders besides the default (easier to > synchronize using subversion). Eg: myproject/rfunctions/myfunctions.R > - Can be accessed from the R console (without having to load a > specific workspace). > > Is there any solution that can provide this? > > Any suggestions would be greatly appreciated, > > Thanks, > Sergio > >And what holds you from doing the steps you described? Create a plain text file with functions (myfunctions.R, as you want), define its location (myproject/rfunctions/ ) and save it there. Then add source("myproject/rfunctions/myfunctions.R") early in every R script or project using those functions. Another variant - you can save the functions with save() in myfunctions.RData file. If you will use save(<...>, ascii=TRUE) this file will not be binary, but it still will have an internal R format, and you won't be able to edit it. Then attach("myproject/rfunctions/myfunctions.RData") You can add calls to source() or attach in the .Rprofile file in your local directory or in Rprofile.site for system wide use, and your functions will appear in R's worspace automatically. Consult with ?Startup for more details. -- View this message in context: http://www.nabble.com/How-to-create-something-between-a-script-and-a-package-tf4591705.html#a13110988 Sent from the R help mailing list archive at Nabble.com.
Uwe Ligges
2007-Oct-09 08:03 UTC
[R] How to create something between a script and a package
Sergio Correia wrote:> (Before starting: I'm a total R noob so please bear with me in case of > any error or faux pas). > > Hi, > > For a small project, I'm writing a few simple R functions and calling > them from python (using RPy). I'm sharing the code with a couple of > friends, using a subversion server. > > Now, I want something like a package, to be able to share functions > between ourselves. Having read a few package tutorials, I feel that > this is not exactly what I want and I'm kinda lost regarding what > alternatives are available. > > What I want could be summarized as this: > - A plain text file (eg: myfunctions.R) > - Can be located in other folders besides the default (easier to > synchronize using subversion). Eg: myproject/rfunctions/myfunctions.R > - Can be accessed from the R console (without having to load a > specific workspace). > > Is there any solution that can provide this?I guess what you want is a package. Otherwise, see ?source. Uwe Ligges> Any suggestions would be greatly appreciated, > > Thanks, > Sergio >
Douglas Bates
2007-Oct-10 17:51 UTC
[R] How to create something between a script and a package
On 10/8/07, Sergio Correia <sergio.correia at gmail.com> wrote:> (Before starting: I'm a total R noob so please bear with me in case of > any error or faux pas). > > Hi, > > For a small project, I'm writing a few simple R functions and calling > them from python (using RPy). I'm sharing the code with a couple of > friends, using a subversion server. > > Now, I want something like a package, to be able to share functions > between ourselves. Having read a few package tutorials, I feel that > this is not exactly what I want and I'm kinda lost regarding what > alternatives are available. > > What I want could be summarized as this: > - A plain text file (eg: myfunctions.R) > - Can be located in other folders besides the default (easier to > synchronize using subversion). Eg: myproject/rfunctions/myfunctions.R > - Can be accessed from the R console (without having to load a > specific workspace). > > Is there any solution that can provide this? > > Any suggestions would be greatly appreciated,As others have suggested, you will probably find that constructing a package is the best choice. However, there is an alternative that has not yet been discussed. You can save one or more functions and data sets to a file (see ?save) then on starting another session attach that file (see ?attach). The process is like foo <- function(x) mean(as.numeric(x), trim = 0.3) save(foo, file = "myproject/rfunctions/saved.rda") then, when starting a new session, use attach("myproject/rfunctions/saved.rda") The .rda extension on the filename is commonly used for saved R data sets but you can also have function definitions in a saved file.