I have several custom functions that I use frequently in R. Rather than souce this file (or parts thereof) in each script, is there some way to add this to a base R file such that they are always available when I use R?
5 Answers
Yes, create a package. There are numerous tutorials as well as the Writing R Extensions manual that came with your copy of R.
It may seem like too much work at first, but you will probably be glad that you did this in the longer run.
PS And you can then load that package from ~/.Rprofile
. For really short code, you can also define it there.

- 360,940
- 56
- 644
- 725
-
3Just compiled my first package. Didn't realize how easy it was. Thanks Dirk! – Maiasaura Jul 08 '10 at 21:00
-
1Glad that ended up as the accepted answer. Packages are easy and provide so many benefits! (e.g. http://stackoverflow.com/questions/2284446/organizing-r-source-code/2284486#2284486) – Shane Jul 08 '10 at 21:30
-
Agreed- R makes creating packages dead simple. One of the easiest languages to extend in my experience. – Sharpie Jul 10 '10 at 04:53
-
Dirk, that link does not work, because R-exts.html is written with a small "e" . The correct link should be: http://cran.r-project.org/doc/manuals/R-exts.html , thx for editing in advance. Nice answer and question – should be correctly archived :) – Matt Bannert Jul 10 '10 at 12:15
A package may be overkill for a for a few useful functions. I'd argue there's nothing wrong with explicitly source()
ing them as you need them - at least it is explicit so that if you email someone your code, you won't forget to include those other scripts.

- 102,019
- 32
- 183
- 245
-
I guess it's annoying to have to load this into every new Rscript you work with ever. – wolfsatthedoor Apr 19 '19 at 23:53
Another option is to use the .Rprofile
file. You can read about the details in ?Startup
. Basically, the idea is that:
...a file called ‘.Rprofile’ is searched for in the current directory or in the user's home directory (in that order). The user profile file is sourced into the workspace.
You can read here about how many people use this functionality.
The accepted answer is best long-term: Make a package.
Luckily, the learning curve for doing this has been dramatically reduced by the devtools
package: It automates package creation (a nice assist in getting off on the right foot), encourages good practices (like documenting with roxygen2
, and helps with using online version control (bitbucket, github or other), sharing your package with others. It's also very helpful for smoothing your way to CRAN submission.
Good docs at http://adv-r.had.co.nz and http://r-pkgs.had.co.nz .
to create your package, for instance you can:
install.packages("devtools")
devtools::create("path/to/package/pkgname")

- 3,559
- 1
- 33
- 46
You could also look at the 'mvbutils' package: it lets you set up a hierarchical set of "tasks" (folders with workspace ".RData" files in them) such that you can always see what's in the ancestral tasks (ie the ancestors are in the search() path). So you can put your custom functions in the "starting task" where you always start R; and then you change to vwhatever project-specific task you require, so you can avoid cluttered workspaces, but you'll still be able to use (and edit) your custom functions because the starting task is always ancestral. Objects (including functions) get stored in ".RData" files and are thus loaded/saved automatically, but there are separate text-backup facilities for functions.
There are lots of different ways of working in R, and no "one-size-fits-all" best solution. It's also not easy to find an overview! Speaking just for myself:
I'm not a fan of having to 'source' everything in every time; for one thing, it simply doesn't work with big data sets and/or results of model runs.
I think packages are hard to create and maintain; there is a really significant overhead. After the first 5 packages you write, it does get a bit easier provided you do it on at least a weekly basis so you don't forget how, but really...
In fact, 'mvbutils' also has a bunch of tools for facilitating the creation and (especially) maintenance of packages, designed to interface smoothly with the task-hierarchy system. I use & edit my own packages all the time (including editing mvbutils itself); but if it wasn't for the tools in 'mvbutils', I'd be grinding my teeth in frustration most days of the week.

- 526
- 4
- 2