Shayne H Shayne H - 4 months ago 19
R Question

Cannot find source for contribUrl function within miniCRAN::pkgAvail call to available.packages

The

miniCRAN
package's
pkgAvail
function is a basically a wrapper to the
available.packages
function. What I'm having trouble with is finding the definition of contribUrl() function call it makes in the repo parameter.
I understand what it is supposed to do - allows for a contrib url of a R version explicitly identified, instead of using R.version.
But where is contribUrl() defined?

available.packages
utils::available.packages(contribUrl(repos, type = type,
Rversion = Rversion), type = type, filters = list())


For context, this is where the function call is from:

pkgAvail <- function (repos = getOption("repos"), type = "source", Rversion = R.version)
{
if (!grepl("^http://|file:///", repos[1]) && file.exists(repos[1])) {
repos <- paste0("file:///", normalizePath(repos[1], mustWork = FALSE,
winslash = "/"))
}
else {
if (!is.null(names(repos)) && isTRUE(unname(repos["CRAN"]) ==
"@CRAN@")) {
repos <- MRAN()
}
}
utils::available.packages(contribUrl(repos, type = type,
Rversion = Rversion), type = type, filters = list())
}

Answer

It's a modified copy of contrib.url from utils. It's defined here:

https://github.com/RevolutionAnalytics/miniCRAN/blob/master/R/pkgDep.R

Here's the definition:

# Modified copy of utils::contrib.url()
contribUrl <- function (repos, type = getOption("pkgType"), Rversion = R.version) {
  Rversion <- twodigitRversion(Rversion)
  if (type == "both") 
    type <- "source"
  if (type == "binary") 
    type <- .Platform$pkgType
  if (is.null(repos)) 
    return(NULL)
  if ("@CRAN@" %in% repos && interactive()) {
    cat(gettext("--- Please select a CRAN mirror for use in this session ---"), 
        "\n", sep = "")
    flush.console()
    chooseCRANmirror()
    m <- match("@CRAN@", repos)
    nm <- names(repos)
    repos[m] <- getOption("repos")["CRAN"]
    if (is.null(nm)) 
      nm <- rep("", length(repos))
    nm[m] <- "CRAN"
    names(repos) <- nm
  }
  if ("@CRAN@" %in% repos) 
    stop("trying to use CRAN without setting a mirror")
  ver <- Rversion
  mac.path <- "macosx"
  if (substr(type, 1L, 11L) == "mac.binary.") {
    mac.path <- paste(mac.path, substring(type, 12L), sep = "/")
    type <- "mac.binary"
  }
  res <- switch(type, 
                source = paste(gsub("/$", "", repos), "src", "contrib", sep = "/"), 
                mac.binary = paste(gsub("/$", "", repos), "bin", mac.path, "contrib", ver, sep = "/"), 
                win.binary = paste(gsub("/$", "", repos), "bin", "windows", "contrib", ver, sep = "/"))
  res
}

Here's the original function:

> utils::contrib.url
function (repos, type = getOption("pkgType")) 
{
    type <- resolvePkgType(type)
    if (is.null(repos)) 
        return(NULL)
    if ("@CRAN@" %in% repos && interactive()) {
        cat(gettext("--- Please select a CRAN mirror for use in this session ---"), 
            "\n", sep = "")
        flush.console()
        chooseCRANmirror()
        m <- match("@CRAN@", repos)
        nm <- names(repos)
        repos[m] <- getOption("repos")["CRAN"]
        if (is.null(nm)) 
            nm <- rep("", length(repos))
        nm[m] <- "CRAN"
        names(repos) <- nm
    }
    if ("@CRAN@" %in% repos) 
        stop("trying to use CRAN without setting a mirror")
    ver <- paste(R.version$major, strsplit(R.version$minor, ".", 
        fixed = TRUE)[[1L]][1L], sep = ".")
    mac.path <- "macosx"
    if (substr(type, 1L, 11L) == "mac.binary.") {
        mac.path <- paste(mac.path, substring(type, 12L), sep = "/")
        type <- "mac.binary"
    }
    res <- switch(type, source = paste(gsub("/$", "", repos), 
        "src", "contrib", sep = "/"), mac.binary = paste(gsub("/$", 
        "", repos), "bin", mac.path, "contrib", ver, sep = "/"), 
        win.binary = paste(gsub("/$", "", repos), "bin", "windows", 
            "contrib", ver, sep = "/"))
    res
}
<bytecode: 0x0000000007708e90>
<environment: namespace:utils>