Skip to content

Commit

Permalink
install_CRAN function
Browse files Browse the repository at this point in the history
  • Loading branch information
jimhester committed Apr 25, 2016
1 parent 1d83903 commit 35d8195
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
58 changes: 58 additions & 0 deletions R/install-CRAN.r
@@ -0,0 +1,58 @@
#' Attempts to install a package from CRAN.
#'
#' This function is vectorised on \code{pkgs} so you can install multiple
#' packages in a single command.
#'
#' @param pkgs Character vector of packages to install.
#' @inheritParams package_deps
#' @export
#' @family package installation
#' @examples
#' \dontrun{
#' install_CRAN("ggplot2")
#' install_CRAN(c("httpuv", "shiny")
#' }
install_CRAN <- function(pkgs, repos = getOption("repos"), type = getOption("pkgType"), ..., quiet = FALSE) {

remotes <- lapply(pkgs, CRAN_remote, repos = repos, type = type)

install_remotes(remotes, quiet = quiet, ...)
}

CRAN_remote <- function(pkg, repos, type) {

remote("CRAN",
name = pkg,
repos = repos,
pkg_type = type)
}


#' @export
remote_download.CRAN_remote <- function(x, quiet = FALSE) {
dest_dir <- tempdir()
download.packages(x$name, destdir = dest_dir, repos = x$repos, type = x$pkg_type)[1, 2]
}

#' @export
remote_metadata.CRAN_remote <- function(x, bundle = NULL, source = NULL) {
version <- read_dcf(file.path(source, "DESCRIPTION"))$Version
list(
RemoteType = "CRAN",
RemoteSha = version,
RemoteRepos = deparse(x$repos),
RemotePkgType = x$pkg_type
)
}

#' @export
remote_package_name.CRAN_remote <- function(remote, ...) {
remote$name
}

#' @export
remote_sha.CRAN_remote <- function(remote, url = "https://github.com", ...) {
cran <- available_packages(remote$repos, remote$pkg_type)

tryCatch(cran[remote$name, "Version"], error = function(e) NA)
}
40 changes: 40 additions & 0 deletions man/install_CRAN.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 35d8195

Please sign in to comment.