Install R package only if not installed

options(echo=TRUE) # if you want see commands in output file
args <- commandArgs(trailingOnly = TRUE)
print(args)
# trailingOnly=TRUE means that only your arguments are returned, check:
# print(commandsArgs(trailingOnly=FALSE))

# Install only if not already installed
# http://stackoverflow.com/questions/9341635/check-for-installed-packages-before-running-install-packages
pkgInstall <- function(x)
{
if (!require(x,character.only = TRUE))
{
install.packages(pkgs = x,lib = '/usr/lib64/R/library',repos = "http://local/cran-mirror",dependencies = TRUE,verbose = FALSE,quiet = FALSE)
if(!require(x,character.only = TRUE)) stop("Package not found")
}
}

pkgInstall(args[1])


Save the above script as installpackage.R

Use it as

sudo Rscript installpackage.R pkgname

No comments:

Post a Comment

Please share your views and comments below.

Thank You.