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.
You shouldn't have to re-install packages each time you open R. If library(dynlm) doesn't work without re-installing then I would say this is definitely an issue. However, you do generally need to load the packages you want to use in that session via library() .
packages() function. You can install multiple packages by passing a vector of package names to the function, for example, install. packages(c("dplyr", "stringr")) . That function will install the requested packages, along with any of their non-optional dependencies.
As far as I know, there are no automatic updates for R, RStudio and packages. And as updates are quite frequent, it is quite a hassle to check every few weeks (or even days) if there are new versions available.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With