Files
Rpacman/R/add_package.R

66 lines
1.7 KiB
R

#' Add a package to the project.
#'
#' @param packageName character Name of the package to add
#' @param installOpts list Options for installing the package
#'
add_package <- function(packageName = NULL, installOpts = list(Ncpus = parallel::detectCores())) {
if (is.null(packageName)) {
repeat {
packageName <- trimws(readline("Please specify the name of the package you wish to add: "))
if (nchar(packageName) > 0) {
break
}
}
}
cranPackages <- fetch_available_packages()
archivePackages <- fetch_available_archives()
if (packageName %in% cranPackages$package) {
repeat({
installCRAN <- trimws(readline("Found package in CRAN, proceed with install? [Yn]: "))
if (nchar(installCRAN) == 0) {
installCRAN <- "Y"
}
if (nchar(installCRAN) == 1 && installCRAN %in% c("Y", "y", "N", "n")) {
break
}
})
if (installCRAN %in% c("Y", "y")) {
do.call(utils::install.packages, c(package, installOpts))
return(TRUE)
}
} else {
cat("Package not found on CRAN.\n")
}
repeat({
installCustom <- trimws(readline("Would you like to provide a source? [Yn]: "))
if (nchar(installCustom) == 0) {
installCustom <- "Y"
}
if (nchar(installCustom) == 1 && installCustom %in% c("Y", "y", "N", "n")) {
break
}
})
if (installCustom %in% c("N", "n")) {
cat("Package installation cancelled.\n")
return(FALSE)
}
source <- request_package_source(package)
if (is.null(source)) {
cat("Package installation cancelled.\n")
return(FALSE)
}
package <- install_package_source(c(package = packageName, source), installOpts)
return(TRUE)
}