Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .Rprofile

This file was deleted.

14 changes: 10 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ Package: adrgOS
Title: Utility functions for ADRG
Version: 0.0.0.9000
Authors@R:
person("First", "Last", , "first.last@example.com", role = c("aut", "cre"),
comment = c(ORCID = "YOUR-ORCID-ID"))
Description: Provide tools to be able to fill in sections into the ADRG file.
person("First", "Last", , "first.last@example.com", role = c("aut", "cre"))
Description: Provide tools to be able to fill in sections into the ADRG
file.
License: MIT + file LICENSE
URL: https://phuse-org.github.io/adrgOS/
Imports:
renv,
utils
Suggests:
testthat
Config/testthat/edition: 3
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
URL: https://phuse-org.github.io/adrgOS/
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Generated by roxygen2: do not edit by hand

export(list_r_packages)
193 changes: 125 additions & 68 deletions R/list_r_packages.r
Original file line number Diff line number Diff line change
@@ -1,85 +1,142 @@
#' List R Packages
#'
#' Retrieves information about R packages from either the current R session
#' or from an renv lockfile.
#'
#' @param use_renv Logical. If TRUE, reads packages from renv lockfile.
#' If FALSE (default), reads from current R session.
#' @param lockfile_loc Character. Path to the renv lockfile. Required when
#' use_renv = TRUE.
#'
#' @return A data.frame with columns Package, Version, and Title.
#' When use_renv = FALSE, returns all packages (loaded + base).
#' When use_renv = TRUE, returns packages from lockfile.
#'
#' @details
#' When use_renv = FALSE:
#' - Retrieves loaded packages from sessionInfo()$loadedOnly
#' - Retrieves base packages from sessionInfo()$basePkgs
#' - Combines both into a single data.frame
#' - Missing package information is replaced with default values
#'
#' When use_renv = TRUE:
#' - Reads package information from the specified renv lockfile
#' - Validates that the lockfile exists and throws error if not found
#' - Returns empty data.frame if no packages found in lockfile
#' - Missing package information is replaced with default values
#'
#' @examples
#' \dontrun{
#' # List packages from current R session
#' packages <- list_r_packages()
#' head(packages)
#'
#' # List packages from current R session (explicit)
#' session_packages <- list_r_packages(use_renv = FALSE)
#' nrow(session_packages)
#'
#' # List packages from renv lockfile
#' lockfile_packages <- list_r_packages(use_renv = TRUE, lockfile_loc = "renv.lock")
#' print(lockfile_packages)
#' }
#'
#' @seealso \code{\link{sessionInfo}}, \code{\link{packageDescription}}
#'
#' @export
list_r_packages <- function(use_renv = FALSE, lockfile_loc = NULL) {
if (use_renv) {
if (is.null(lockfile_loc)) {
stop("lockfile_loc must be provided when use_renv = TRUE")
}

if (use_renv == TRUE) {
if (!file.exists(lockfile_loc)) {
stop("renv lockfile not found at: ", lockfile_loc)
}

# renv code
lockfile_path <- lockfile_loc
lockfile <- renv::lockfile_read(lockfile_loc)
packages <- lockfile$Packages

if (file.exists(lockfile_path)) {
lockfile <- renv::lockfile_read(lockfile_path)
packages <- lockfile$Packages
return(
get_package_info(renv_packages = packages)
)
} else {
session_info <- utils::sessionInfo()
loaded_packages <- names(session_info$loadedOnly)
base_packages <- session_info$basePkgs

if (!is.null(packages) && length(packages) > 0) {
package_list <- data.frame(
Package = names(packages),
Version = sapply(packages, function(x) x$Version),
Title = sapply(packages, function(x) x$Title),
stringsAsFactors = FALSE
)
print(package_list)
} else {
print("No packages found in the lockfile.")
}
} else {
print("renv lockfile not found. Make sure renv has been initialized.")
}
}
else if (use_renv == FALSE) {
# Non-renv code
loaded_packages <- names(sessionInfo()$loadedOnly)
loaded_info <- get_package_info(pkg_names = loaded_packages)
base_info <- get_package_info(pkg_names = base_packages)

package_info <- data.frame(
Package = character(),
Version = character(),
Title = character(),
stringsAsFactors = FALSE
return(
rbind(loaded_info, base_info)
)
}
}

for (pkg in loaded_packages) {
pkg_desc <- packageDescription(pkg)
pkg_name <- pkg_desc$Package
pkg_version <- pkg_desc$Version
pkg_title <- pkg_desc$Title
#' Null Coalescing Operator
#'
#' @noRd
`%||%` <- function(x, y) if (is.null(x)) y else x

package_info <- rbind(package_info, data.frame(
Package = pkg_name,
Version = pkg_version,
Title = pkg_title,
stringsAsFactors = FALSE
))
}

print(package_info)

base_packages <- sessionInfo()$basePkgs
#' Get Package Information
#'
#' Helper function to extract package information from package names or renv packages.
#'
#' @param pkg_names Character vector of package names (for session packages)
#' @param renv_packages List of renv package objects (for renv packages)
#'
#' @return A data.frame with columns Package, Version, and Title.
#' Missing information is replaced with default values.
#'
#' @keywords internal
get_package_info <- function(pkg_names = NULL, renv_packages = NULL) {
if (!is.null(renv_packages)) {
# Handle renv packages
if (length(renv_packages) == 0) {
return(
data.frame(
Package = character(),
Version = character(),
Title = character(),
stringsAsFactors = FALSE
)
)
}

base_package_info <- data.frame(
Package = character(),
Version = character(),
Title = character(),
stringsAsFactors = FALSE
return(
data.frame(
Package = names(renv_packages),
Version = sapply(renv_packages, function(x) x$Version %||% "Unknown"),
Title = sapply(renv_packages, function(x) {
x$Title %||% "No title available"
}),
stringsAsFactors = FALSE
)
)
}

for (pkg in base_packages) {
pkg_desc <- packageDescription(pkg)
pkg_name <- pkg_desc$Package
pkg_version <- pkg_desc$Version
pkg_title <- pkg_desc$Title

base_package_info <- rbind(base_package_info, data.frame(
Package = pkg_name,
Version = pkg_version,
Title = pkg_title,
# Handle session packages
if (is.null(pkg_names) || length(pkg_names) == 0) {
return(
data.frame(
Package = character(),
Version = character(),
Title = character(),
stringsAsFactors = FALSE
))
}

print("Base Packages:")
print(base_package_info)
)
)
}

all_packages_info <- rbind(package_info, base_package_info)
pkg_data <- lapply(pkg_names, function(pkg) {
pkg_desc <- utils::packageDescription(pkg)
data.frame(
Package = pkg_desc$Package %||% pkg,
Version = pkg_desc$Version %||% "Unknown",
Title = pkg_desc$Title %||% "No title available",
stringsAsFactors = FALSE
)
})

print("All Packages (Base and Loaded):")
print(all_packages_info)
}
do.call(rbind, pkg_data)
}
12 changes: 12 additions & 0 deletions dev/config_attachment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
path.n: NAMESPACE
path.d: DESCRIPTION
dir.r: R
dir.v: vignettes
dir.t: tests
extra.suggests: ~
pkg_ignore: ~
document: yes
normalize: yes
inside_rmd: no
must.exist: yes
check_if_suggests_is_installed: yes
21 changes: 21 additions & 0 deletions man/get_package_info.Rd

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

60 changes: 60 additions & 0 deletions man/list_r_packages.Rd

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

Loading
Loading