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.
How to load packages automatically when opening a project in RStudio
I presume you want to say that you have to reload all of the packages that were loaded in the workspace previously. That's not an error, that's by design.
If you want to load some packages at startup in a project, you can do so by creating a file called .Rprofile
in the project directory, and specify whatever code you want RStudio to run when loading the project.
For example:
cat("Welcome to this project.\n")
require(ggplot2)
require(zoo)
would print a welcome message in the console, and load ggplot2
and zoo
every time you open the project.
See also http://www.rstudio.com/ide/docs/using/projects
Elegant way to check for missing packages and install them?
Yes. If you have your list of packages, compare it to the output from installed.packages()[,"Package"]
and install the missing packages. Something like this:
list.of.packages <- c("ggplot2", "Rcpp")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
Otherwise:
If you put your code in a package and make them dependencies, then they will automatically be installed when you install your package.
How to load packages automatically when opening a project in RStudio
I presume you want to say that you have to reload all of the packages that were loaded in the workspace previously. That's not an error, that's by design.
If you want to load some packages at startup in a project, you can do so by creating a file called .Rprofile
in the project directory, and specify whatever code you want RStudio to run when loading the project.
For example:
cat("Welcome to this project.\n")
require(ggplot2)
require(zoo)
would print a welcome message in the console, and load ggplot2
and zoo
every time you open the project.
See also http://www.rstudio.com/ide/docs/using/projects
Loading R packages automatically while preserving the portability of my code
After discussing this question on Software Engineer Stackexchange, I decided to use Imports:magrittr
as the smallest and most widely used package that loads all of my custom package dependencies.
Related Topics
Long/Bigint/Decimal Equivalent Datatype in R
What Is the Purpose of Setting a Key in Data.Table
Read Multiple CSV Files into Separate Data Frames
Using Data.Table Package Inside My Own Package
Select Groups With More Than One Distinct Value
Starting Shiny App After Password Input
How to Format a Number as Percentage in R
Pasting Two Vectors With Combinations of All Vectors' Elements
Calculate Cumulative Sum (Cumsum) by Group
Plot Multiple Lines in One Graph
How to Efficiently Calculate Distance Between Pair of Coordinates Using Data.Table :=
Create Group Number For Contiguous Runs of Equal Values
How to Unload a Package Without Restarting R