[R] How to create something between a script and a package

Douglas Bates
Wed, 10 Oct 2007 17:51:11 -0000

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.