[R] Preventing repeated package installation, or pre installing packages

Larry Martell larry.martell at gmail.com
Fri Dec 8 02:29:05 CET 2017

On Wed, Nov 29, 2017 at 11:14 AM, Rainer Krug <Rainer at krugs.de> wrote:
> On 29 Nov 2017, at 15:28, Larry Martell <larry.martell at gmail.com> wrote:
> I have a R script that I call from python using rpy2. It uses dplyr, doBy,
> and ggplot2. The script has install.packages commands for these 3 packages.
> Even thought the packages are already installed it still downloads,
> builds, and installs them, which is very time consuming. Is there a way to
> have it only do the install if the package is not already installed?
> You could use something like
> if (!require(dplyr)) {
> install.packages(“dplyr”)
> library(dplyr)
> }
> where require() returns FALSE if it fails to load the package.

Thanks that worked perfectly.

> Also, I run in a docker container, so after the container is instantiated
> the packages are not there the first time the script runs. Is there a way
> to pre load the packages, in which case I would not need the
> install.packages commands for these packages and my above question would
> become moot.
> Yes - add them to you Docker file, but this is a docker question, not R.
> Check out the Rocker Dockerfiles to see how you can do this.

What I did not know was how to load a R package from the command line.
Thanks to Thierry Onkelinx's answer I now do know.

More information about the R-help mailing list