From 11d7d5c342161a081b3d161995499a8afd8fbe4f Mon Sep 17 00:00:00 2001 From: karissawhiting Date: Thu, 25 Apr 2024 16:39:36 -0400 Subject: [PATCH] add packages --- .Rbuildignore | 2 ++ .gitignore | 1 + DESCRIPTION | 23 +++++++++++++ NAMESPACE | 2 ++ R/attach.R | 83 +++++++++++++++++++++++++++++++++++++++++++++++ R/utils.R | 55 +++++++++++++++++++++++++++++++ R/zzz.R | 12 +++++++ genomeverse.Rproj | 22 +++++++++++++ 8 files changed, 200 insertions(+) create mode 100644 .Rbuildignore create mode 100644 .gitignore create mode 100644 DESCRIPTION create mode 100644 NAMESPACE create mode 100644 R/attach.R create mode 100644 R/utils.R create mode 100644 R/zzz.R create mode 100644 genomeverse.Rproj diff --git a/.Rbuildignore b/.Rbuildignore new file mode 100644 index 0000000..a4ef341 --- /dev/null +++ b/.Rbuildignore @@ -0,0 +1,2 @@ +^genomeverse\.Rproj$ +^\.Rproj\.user$ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cd67eac --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.Rproj.user diff --git a/DESCRIPTION b/DESCRIPTION new file mode 100644 index 0000000..07272b3 --- /dev/null +++ b/DESCRIPTION @@ -0,0 +1,23 @@ +Package: genomeverse +Title: What the Package Does (One Line, Title Case) +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: What the package does (one paragraph). +License: `use_mit_license()`, `use_gpl3_license()` or friends to pick a + license +Encoding: UTF-8 +Roxygen: list(markdown = TRUE) +RoxygenNote: 7.2.3 +Imports: + cbioportalR, + cli, + crayon, + gnomeR (>= 1.2.0.9004), + oncokbR (>= 0.0.0.9001), + rstudioapi, + utils +Remotes: + MSKCC-Epi-Bio/gnomeR, + karissawhiting/oncokbR diff --git a/NAMESPACE b/NAMESPACE new file mode 100644 index 0000000..6ae9268 --- /dev/null +++ b/NAMESPACE @@ -0,0 +1,2 @@ +# Generated by roxygen2: do not edit by hand + diff --git a/R/attach.R b/R/attach.R new file mode 100644 index 0000000..1c30df2 --- /dev/null +++ b/R/attach.R @@ -0,0 +1,83 @@ + +pkgs <- c("cbioportalR", "gnomeR", "oncokbR") + +genomeverse_attach <- function() { + # Create `to_load` which is a character vector of all genomeverse + # packages not loaded in the current R session. + to_load <- check_loaded() + + # If to_load has length 0, all main packages are loaded. + # Nothing will be attached. + if (length(to_load) == 0) { + return(invisible()) + } + + # Create a line rule with two text labels: + # "Attaching packages" on the left-hand side and + # genomeverse with the package version on the right-hand side + load_header <- cli::rule( + left = crayon::bold("Attaching packages"), + right = paste0("genomeverse ", package_version("genomeverse")) + ) + + # Return a character string containing the package version for each of genomeverse's constituents + versions <- vapply(to_load, package_version, character(1)) + + packages <- paste0( + crayon::green(cli::symbol$tick), " ", crayon::blue(format(to_load)), " ", + crayon::col_align(versions, max(crayon::col_nchar(versions))) + ) + + # Format for two columns + # if there is an odd number of packages, add "" + if (length(packages) %% 2 == 1) { + packages <- append(packages, "") + } + # Divide the packages into column 1 and 2 + col1 <- seq_len(length(packages) / 2) + # paste the packages in column one with a space and those not in column 1 + info <- paste0(packages[col1], " ", packages[-col1]) + + # display the message! + msg(load_header) + msg(paste(info, collapse = "\n")) + + # Load the constituent packages! + # character.only = TRUE must be used in order to + # supply character strings to `library()` + suppressPackageStartupMessages( + lapply(to_load, library, character.only = TRUE) + ) + + # Thanks for playing + invisible() + +} + +# Detach all loaded packages for seeing the pretty startup message (: +genomeverse_detach <- function() { + pak <- paste0("package:", c(pkgs, "genomeverse")) + lapply(pak[pak %in% search()], detach, character.only = TRUE) + invisible() +} + +#' List all packages imported by genomeverse +#' +#' @export +#' +#' @examples +#' genomeverse_packages() +genomeverse_packages <- function() { + # get all imports from genomeverse's package description file + raw <- utils::packageDescription("genomeverse")$Imports + # return a character vector of all the imports + imports <- strsplit(raw, ",")[[1]] + # "^\\s+" matches white space at the beginning of a character string + # "\\s+$ matches white space at the end of a character string + parsed <- gsub("^\\s+|\\s+$", "", imports) + # for each import, take only the first complete word (i.e. the package name) + names <- vapply(strsplit(parsed, "\\s+"), "[[", 1, FUN.VALUE = character(1)) + + return(names) + +} diff --git a/R/utils.R b/R/utils.R new file mode 100644 index 0000000..375498f --- /dev/null +++ b/R/utils.R @@ -0,0 +1,55 @@ + +text_col <- function(x) { + + # If RStudio API is not available and/or does not have the getThemeInfo + # button, exit function leaving default color of black + if (!rstudioapi::isAvailable() || !rstudioapi::hasFun("getThemeInfo")) { + return(x) + } + + # Get theme information for RStudio + theme <- rstudioapi::getThemeInfo() + + # If it's a dark theme, make the text color white; otherwise black. + if (isTRUE(theme$dark)) crayon::white(x) else crayon::black(x) + +} + +# Format the package version to indicate development versions when printed on +# the command line +package_version <- function(x) { + # packageVersion returns an object with class 'package_version' and 'numeric_version' + # unclass removes those and it becomes a numeric + version <- unclass(utils::packageVersion(x))[[1]] + + # if the length of the numeric version vector is > 3, + # as happens with development packages, coerce those + # dev version numbers to red + if (length(version) > 3) { + version[4:length(version)] <- crayon::red(as.character(version[4:length(version)])) + } + + # concatenate the result + paste0(version, collapse = ".") + +} + +# Create a message function for start-up that dynamically changes text color +msg <- function(...) { + packageStartupMessage(text_col(...)) +} + + +# Check which of the main genomeverse packages +# are currently loaded +# The search() function returns a character vector containing packages +# attached to the current R session. +check_loaded <- function() { + paks <- paste0("package:", pkgs) + pkgs[!paks %in% search()] +} + +# Is a package attached? +is_attached <- function(x) { + paste0("package:", x) %in% search() +} diff --git a/R/zzz.R b/R/zzz.R new file mode 100644 index 0000000..475dfe7 --- /dev/null +++ b/R/zzz.R @@ -0,0 +1,12 @@ +.onAttach <- function(...) { + # See if any packages are needed + needed <- pkgs[!is_attached(pkgs)] + # If no packages are needed, return + if (length(needed) == 0) { + return() + # Otherwise, attach any needed packages + } else { + genomeverse_attach() + } + +} diff --git a/genomeverse.Rproj b/genomeverse.Rproj new file mode 100644 index 0000000..69fafd4 --- /dev/null +++ b/genomeverse.Rproj @@ -0,0 +1,22 @@ +Version: 1.0 + +RestoreWorkspace: No +SaveWorkspace: No +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX + +AutoAppendNewline: Yes +StripTrailingWhitespace: Yes +LineEndingConversion: Posix + +BuildType: Package +PackageUseDevtools: Yes +PackageInstallArgs: --no-multiarch --with-keep.source +PackageRoxygenize: rd,collate,namespace