How to load packages in R automatically?
Put library(foo)
in your .Rprofile
file or set R_DEFAULT_PACKAGES
: see ?Rprofile
...
In particular (because ?Rprofile
is long and potentially intimidating):
If you want a different set of packages than the default ones when you start, insert a call to ‘options’ in the ‘.Rprofile’ or ‘Rprofile.site’ file. For example, ‘options(defaultPackages = character())’ will attach no extra packages on startup (only the ‘base’ package) (or set ‘R_DEFAULT_PACKAGES=NULL’ as an environment variable before running R). Using ‘options(defaultPackages = "")’ or ‘R_DEFAULT_PACKAGES=""’ enforces the R system default.
Since you probably do want all of the default packages loaded, and then extra ones in addition (rather than, say, not loading some of the default packages), you can either put
library("mypackage1")library("mypackage2")[etc.]
or using options(defaultPackages=...)
:
options(defaultPackages=c(getOption("defaultPackages"), "mypackage1","mypackage2", ... [etc.]))
in your .Rprofile
to append your desired packages to the standard defaults.
edit (copied from comment) re getting this to work in Rstudio: http://rstudio.org/docs/using/workspaces suggests that Rstudio executes .Rprofile
and then "Performs the other actions described in R Startup [ http://stat.ethz.ch/R-manual/R-patched/library/base/html/Startup.html ]" (which is the same as ?Rprofile
). It is ambiguous whether it looks at Rprofile.site
or not.
edit #2: according to comment below, it does work with a recent version of Rstudio.
Quick-R page on customizing R startup contains basically the same information than in Ben's and Joao's answers, but it is perhaps a bit clearer. Create a copy of Rprofile.site
file with desired changes in your home folder (Documents on Windows) and call it .Rprofile
EDIT: I noticed that R 3.0.0 does not look from Documents folder any longer, but uses user's home directory (user name) under Windows 7. This might be an installation issue, though (i.e. that I happened to install R "wrongly" previously). However, the Quick-R page linked in this answer tells the right way of doing this. If somebody else is encountering this problem, the solution is to copy .Rprofile
to the user's home directory.