Function to grab the available packages listed on CRAN

This commit is contained in:
2022-06-16 11:27:01 +01:00
parent 6ea4f81bc9
commit 15ded69426
7 changed files with 117 additions and 2 deletions

View File

@@ -1,3 +1,4 @@
# Generated by roxygen2: do not edit by hand
export(fetch_available_packages)
export(list_depends)

View File

@@ -0,0 +1,41 @@
# Requires utils.R
.fetch_available_packages <- function(refetch) {
check_pacman_dir()
fn <- ".pacman/packages.rds"
if (file.exists(fn)) {
ts <- as.POSIXct("1970-01-01 00:00:00", tz = "GMT") + as.numeric(Sys.Date() + 1) * 86400
if (refetch || (ts < file.mtime(fn))) {
unlink(fn)
}
}
if (!file.exists(fn)) {
download.file(sprintf("%s/src/contrib/PACKAGES.rds", get_cran_repo()), fn)
}
con <- gzfile(fn, "rb")
on.exit(close(con), add = TRUE)
packages <- readRDS(con)
packages <- as.data.frame(packages)
packages <- packages[is.na(packages$Path),]
packages <- packages[,c("Package", "Version")]
colnames(packages) <- c("package", "version")
rownames(packages) <- 1:nrow(packages)
return(packages[,c("package", "version")])
}
#' Fetch the current (or check the cache) list of packages available for install
#' from the CRAN repositories.
#'
#' @param refetch Boolean indicating if the local cache should be invalidated.
#'
#' @return A data frame of available packages and their versions
#' @export
fetch_available_packages <- function(refetch = FALSE) {
tryCatch(
{
.fetch_available_packages(refetch)
},
warning = function(e) list(),
error = function(e) list()
)
}

24
R/utils.R Normal file
View File

@@ -0,0 +1,24 @@
#' Checks the current project has a valid pacman directory structure
#'
#' @return invisible boolean TRUE if the structure is valid, FALSE otherwise
check_pacman_dir <- function() {
if (!dir.exists("./.pacman")) {
if (!dir.create("./.pacman")) {
return(invisible(FALSE))
}
}
invisible(TRUE)
}
#' Returns the CRAN repository source or requests the user select one
#'
#' @return string CRAN repository URL
get_cran_repo <- function() {
repo <- getOption("repos")[[1]]
if (repo == "@CRAN@") {
chooseCRANmirror()
repo <- getOption("repos")[[1]]
}
repo
}

14
man/check_pacman_dir.Rd Normal file
View File

@@ -0,0 +1,14 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/utils.R
\name{check_pacman_dir}
\alias{check_pacman_dir}
\title{Checks the current project has a valid pacman directory structure}
\usage{
check_pacman_dir()
}
\value{
invisible boolean TRUE if the structure is valid, FALSE otherwise
}
\description{
Checks the current project has a valid pacman directory structure
}

View File

@@ -0,0 +1,19 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/fetch_available_packages.R
\name{fetch_available_packages}
\alias{fetch_available_packages}
\title{Fetch the current (or check the cache) list of packages available for install
from the CRAN repositories.}
\usage{
fetch_available_packages(refetch = FALSE)
}
\arguments{
\item{refetch}{Boolean indicating if the local cache should be invalidated.}
}
\value{
A data frame of available packages and their versions
}
\description{
Fetch the current (or check the cache) list of packages available for install
from the CRAN repositories.
}

14
man/get_cran_repo.Rd Normal file
View File

@@ -0,0 +1,14 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/utils.R
\name{get_cran_repo}
\alias{get_cran_repo}
\title{Returns the CRAN repository source or requests the user select one}
\usage{
get_cran_repo()
}
\value{
string CRAN repository URL
}
\description{
Returns the CRAN repository source or requests the user select one
}

View File

@@ -2,7 +2,8 @@
% Please edit documentation in R/list_depends.R
\name{list_depends}
\alias{list_depends}
\title{Recursively find all the installed dependencies of one or more packages.}
\title{Recursively find all the installed dependencies of one or more installed
packages.}
\usage{
list_depends(depends)
}
@@ -13,5 +14,6 @@ list_depends(depends)
A data frame with the dependency tree
}
\description{
Recursively find all the installed dependencies of one or more packages.
Recursively find all the installed dependencies of one or more installed
packages.
}