kittygirl
kittygirl

Reputation: 2443

How to quit R script when one R package not installed successfully?

#!/usr/bin/Rscript
install.packages("Deriv")
install.packages("vegan")
packageurl <- "https://cran.r-project.org/src/contrib/Archive/mirt/mirt_1.27.1.tar.gz"
install.packages(packageurl, repos=NULL, type="source")

Above script is used to install packages.

How to make this script stop running (or quit) if anyone package is not installed successfully (warnings do not matter)?

Upvotes: 1

Views: 1415

Answers (2)

Roman Luštrik
Roman Luštrik

Reputation: 70653

I would check installed packages after every install and use stop to stop the script with a meaningful message. E.g.

install.packages("Deriv")

if (!"Deriv" %in% installed.packages()[, "Package"]) {
  stop("Package Deriv not installed successfully.")
}

Upvotes: 2

Paweł Chabros
Paweł Chabros

Reputation: 2399

Is this what you are looking for?

if (
  !all(
    c('Deriv', 'vegan', 'mirt') %in% installed.packages()
  )
) q()

Upvotes: 0

Related Questions