[R] Storing and managing custom R functions for re-use
Barry Rowlingson
b.rowlingson at lancaster.ac.uk
Sat Jul 9 16:21:28 CEST 2011
On Sat, Jul 9, 2011 at 2:37 PM, Abhijit Dasgupta, PhD
<aikidasgupta at gmail.com> wrote:
> I think most of us are in a similar situation. I've usually kept mine in a
> file which is sourced when I start R. The main problem I have with this is
> that it clutters up my environment with a lot of stuff I don't need all the
> time. I'm in the process of creating a custom package which will be
> lazy-loaded. I believe a previous discussion of this topic suggested this as
> the preferred method.
>
If you dont want to make a package there's a few ways of doing this
without cluttering up your environment. For example:
* Create a functions.RData from your functions.R file whenever you
edit your functions.R file, by starting a vanilla R session, sourcing
functions.R, then using save. Then in your working sessions just
attach/detach the functions.RData file.
* Create a new environment, source functions.R into it, and attach
it. Something like:
e = new.env()
with(e,source("functions.R",local=TRUE))
attach(e)
now the functions defined in functions.R are in the 'e' environment,
and since e is attached R will find them.
Some R authors have written systems for doing this kind of thing,
with easy ways of updating when the source changes. See, I think,
mvbutils on CRAN, for this kind of thing on steroids.
Barry
More information about the R-help
mailing list