Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in R Programming by (5.3k points)

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 a vector of several packages).

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

1 Answer

0 votes
by

To check if a package is installed or not, before running install.packages(),  you can do the following:

"xtable" %in% rownames(installed.packages())

The output is TRUE if the package is already installed, otherwise FALSE.

In your case:

if("xtable" %in% rownames(installed.packages()) == FALSE) 

{install.packages("xtable")

  }

The code above will check if the package is already installed or not. It will skip the code in the If statement if the condition evaluates to TRUE, otherwise, it will install the required package.

Browse Categories

...