Check for Installed Packages Before Running Install.Packages()

Check for installed packages before running install.packages()

try: require("xtable") or "xtable" %in% rownames(installed.packages())

R check list of packages, then load or install

One way you could achieve this is by checking if it is installed using the installed.packages() function. Then, you can also check which packages are loaded by using the .packages() function.

When you implement this, it'd look like this:

# Declare packages
packages <- c("dplyr","ggplot2","magrittr")

# Loop through each package
for (package in packages) {

# Install package
# Note: `installed.packages()` returns a vector of all the installed packages
if (!package in installed.packages()) {

# Install it
install.packages(
package,
dependencies = TRUE
)

}

# Load package
# Note: `.packages()` returns a vector of all the loaded packages
if (!package in .packages()) {

# Load it
library(
package,
character.only = TRUE
)

}

}

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 make if statement to check whether a R package is installed or not in R

You are looking for installed.packages() That will list all installed packages. Another option is require(thepackage) which will either load the package or return FALSE if the 'thepackage' is not available.

Check if R package is installed then load library

Use library(x,character.only=TRUE). Also you don't need the last line as suppressPackageStartupMessages(library(x,character.only=TRUE)) already loads the package.

EDIT: @LarsKotthoff is right, you already load the package inside of the if brackets. There you already use option character.only=TRUE so everything is good if you just remove last to lines of your function body.

How to check if a module is installed in Python and, if not, install it within the code?

EDIT - 2020/02/03

The pip module has updated quite a lot since the time I posted this answer. I've updated the snippet with the proper way to install a missing dependency, which is to use subprocess and pkg_resources, and not pip.

To hide the output, you can redirect the subprocess output to devnull:

import sys
import subprocess
import pkg_resources

required = {'mutagen', 'gTTS'}
installed = {pkg.key for pkg in pkg_resources.working_set}
missing = required - installed

if missing:
python = sys.executable
subprocess.check_call([python, '-m', 'pip', 'install', *missing], stdout=subprocess.DEVNULL)

Like @zwer mentioned, the above works, although it is not seen as a proper way of packaging your project. To look at this in better depth, read the the page How to package a Python App.

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

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.

Prompt user prior to installing R package

You can use function menu() inside an if statement to make a simple confirmation mechanism. This will work:

package <- "foo"

if (menu(c("Yes", "No"),
title= paste("Are you sure you want to install package", package)) == "1") {
install.packages(package)
} else { print("Cancelling installation")}


Related Topics



Leave a reply



Submit