Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Check for installed packages before running install.packages() [duplicate]

I have an R script that is shared with several users on different computers. One of its lines contains the install.packages("xtable") command.

The problem is that every time someone runs the script, R spends a great deal of time apparently reinstalling the package (it actually does take some time, since the real case has vector of several packages).

How can I make first check if the packages are installed and then only run install.packages() for the ones that are not?
by

2 Answers

vishaljlf39
try: require("xtable") or "xtable" %in% rownames(installed.packages())
RoliMishra
If you want to do it as simply as possible:

packages <- c("ggplot2", "dplyr", "Hmisc", "lme4", "arm", "lattice", "lavaan")

install.packages(setdiff(packages, rownames(installed.packages())))

Login / Signup to Answer the Question.