Could you suggest me a way for loading packages in R automatically? I mean, I want to start a session in R without needing to use library('package name')
several times. Suppose I downloaded all packages I'll want to use the next time I start R.
-
6Normally this is not a good idea because it makes all subsequent code in the session dependent on your setup whereas if it had the library calls in it then it would be more self-contained. If you try to share your code with others they will not be very happy with you since they won't be able to run your scripts without additional instructions. – G. Grothendieck Apr 24 '12 at 16:48
-
1You could define a `load_extra_packages()` function in your `.Rprofile`/`Rprofile.site` file that loaded all of the packages you wanted (and printed out their names), then run that command at the beginning of your sessions -- that would make your code transparent, if not completely self-contained ... – Ben Bolker Apr 24 '12 at 19:28
4 Answers
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.

- 211,554
- 25
- 370
- 453
-
2+1 for 'because `?Rprofile` is long and potentially intimidating'. It is that, and also a terrifically compact *and* complete piece of documentation. Definitely recommended reading. – Josh O'Brien Apr 24 '12 at 15:30
-
Thanks @Ben Bolker I tried adding this line `library(foo)` in the `Rprofile.site` file and I'm no getting what I want. What do you mean with `foo`? What is that? Is it supposed to be a name for a determined package I want to load? – nhern121 Apr 24 '12 at 17:16
-
4sorry; `foo` is an old-fashioned "placeholder" variable. For example, you might try `library("ggplot2"); library("emdbook"); library("quantreg"); library("rms")` (as examples of some real packages you might want to include). – Ben Bolker Apr 24 '12 at 17:27
-
Ok I appreciate your help in this case @BenBolker. Do you know how to do the same but with Rstudio? – nhern121 Apr 24 '12 at 17:37
-
I would expect that Rstudio would respect your Rprofile.site settings, but to be honest I'm not sure. Have you confirmed that you've got something working with the standard Rgui and that it *doesn't* work with Rstudio? – Ben Bolker Apr 24 '12 at 17:43
-
Yes @BenBolker, I tried putting this line `options(defaultPackages=c(getOption("defaultPackages"), "tm","plyr","twitteR","sqldf"))` in my `Rprofile.site` and it's worked great using the standard Rgui. However, when I start Rstudio, those packages are not loaded. Many thanks! – nhern121 Apr 24 '12 at 17:52
-
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. I would (1) try putting the startup code in your `.Rprofile` (rather than `Rprofile.site`); if that doesn't work, (2) update your question above to reflect this issue, and (3) ask at http://support.rstudio.org/ (referencing this question) – Ben Bolker Apr 24 '12 at 19:25
-
Many thanks once again @BenBolker. I solved this issue by updating my Rstudio version and using the line `options(defaultPackages=c(getOption("defaultPackages"), "tm","plyr","twitteR","sqldf"))` in my Rprofile.site as you suggested me. – nhern121 Apr 24 '12 at 22:20
-
@BenBolker, this doesn't seem to work with packages with inter-dependencies. Say package `A` depends on package `B` (with the DESCRIPTION file reflecting this). Putting `library(A)` in the .Rprofile script causes an error to occur when installing package `B`. The same error occurs if I include `library(B)` or change the `defaultPackages` option instead. The exact error when installing package `B` is `Error: package ‘B’ is required by ‘A’ so will not be detached`. – Jon Claus Jan 16 '14 at 23:03
-
This strikes me as a little bit of a "well, don't do that then" situation ... I would normally expect that you would install packages **before** putting them in your `.Rprofile`. If you have a sensible use case for this, I would suggest asking it as another question ... – Ben Bolker Jan 16 '14 at 23:06
-
I was using it during debugging. Essentially, I would edit and re-install the library frequently. Having the libraries automatically load saves me time when beginning a new R session. – Jon Claus Jan 21 '14 at 15:16
-
Indeed, R (studio) will load base packages after reading .Rprofile, so updating packages is safer, see my post below. – Matifou Feb 22 '18 at 23:34
-
Execute path.expand("~") to see where you need to put your ..Rprofile file – peter2108 Mar 03 '20 at 13:23
There is a file called .Rprofile that is nothing but a script that is run everytime you start a new session of R.
What you need to do is add library(package)
to it. If you're using Unix, it's probably on your home folder as a hidden file.

- 8,696
- 11
- 41
- 65
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.

- 7,530
- 8
- 55
- 92
The quick answer is that you should put your R packages in the .Rprofile
file, as everyone suggested.
Note however that R will read this file, and then load the R base packages. See from ?Startup:
Note that when the site and user profile files are sourced only the base package is loaded,
This can cause problems if the package you want to load enhances/overwrite some R base functions. See for example with tidyverse::filter: https://github.com/tidyverse/dplyr/issues/1611
I can see two solutions so far:
Use
.First.sys()
at the first line of your.Rprofile
file: this is the command that is usually run after reading the .Rprofile, that loads the packages ingetOption("defaultPackages")
.Update the option defaultPackages: don't use
library()
in your .Rprofile, but something like.old <- getOption("defaultPackages") options(defaultPackages = c(old, "tidyverse"))

- 7,968
- 3
- 47
- 52
-
2This answer identified something that others did not: the importance of the order of loading and it's impact on overwriting of functions (e.g. `plot` vs `sp::plot`). The `.First.sys()` solution saved me a lot of headache... Thanks! – brotherJ4mes Jul 03 '20 at 20:31