Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if a certain package was already installed?

Tags:

r

rstudio

When I install the yaml package, an annoying error message pops up in RStudio if it had been previously installed. How can I tell if the package was already installed so I can decide in my code whether to install the package or not?

The message is in a pop up window and it is:

One or more of the packages that will be updated by this installation are currently loaded. Restarting R prior to updating these packages is strongly recommended. RStudio can restart R and then automatically continue the installation after restarting (all work and data will be preserved during the restart). Do you want to restart R prior to installing?

like image 879
kng Avatar asked Jul 16 '13 17:07

kng


People also ask

How do you check if a specific package is installed?

The dpkg-query command can be used to show if a specific package is installed in your system. To do it, run dpkg-query followed by the -l flag and the name of the package you want information about.

How do you check if a specific package is installed in R?

packages() Calling installed. packages() returns a detailed data frame about installed packages, not only containing names, but also licences, versions, dependencies and more.

How do I list installed packages into my system?

You can either use the dpkg command's log or the apt command's log. You'll have to use grep command to filter the result to list the installed packages only. This will list all the packages including the dependencies that were installed recently on your system along with the time of installation.


2 Answers

This will load yaml, installing it first if its not already installed:

if (!require(yaml)) {
  install.packages("yaml")
  library(yaml)
}

or if you want to parameterize it:

pkg <- "yaml"
if (!require(pkg, character.only = TRUE)) {
  install.packages(pkg)
  if (!require(pkg, character.only = TRUE)) stop("load failure: ", pkg)
}

UPDATE. Parametrization.

like image 170
G. Grothendieck Avatar answered Sep 22 '22 06:09

G. Grothendieck


you can use installed.packages() to find installed packages

like image 24
Jilber Urbina Avatar answered Sep 22 '22 06:09

Jilber Urbina