| Title: | Some Utilities & Base Supports for 'SigBridgeR' |
|---|---|
| Description: | Provides fundamental function support for 'SigBridgeR' and its single-cell phenotypic screening algorithm, including optional functions. |
| Authors: | Yuxi Yang [cre, aut] (ORCID: <https://orcid.org/0009-0006-1329-1224>) |
| Maintainer: | Yuxi Yang <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.2.6 |
| Built: | 2026-05-13 06:48:25 UTC |
| Source: | https://github.com/cran/SigBridgeRUtils |
Wraps CLI functions to automatically prepend the caller's identity (function name or 'global') to the output message.
AddCaller2cli(cli_func)AddCaller2cli(cli_func)
cli_func |
A CLI function (e.g., |
A wrapper function that formats output as "[caller]: message".
Adds arbitrary data to the @misc slot of a Seurat object with automatic key
conflict resolution. If the key already exists, automatically appends a numeric
suffix to ensure unique key naming (e.g., "mykey_1", "mykey_2").
AddMisc( seurat_obj, ..., # key = value cover = TRUE # overwrite existing data )AddMisc( seurat_obj, ..., # key = value cover = TRUE # overwrite existing data )
seurat_obj |
A Seurat object to modify |
... |
key-value pairs to add to the |
cover |
Logical indicating whether to overwrite existing data. If (default TRUE). |
The modified Seurat object with added @misc data. The original object
structure is preserved with no other modifications.
If key doesn't exist: uses as-is
If key exists: appends the next available number (e.g., "key_1", "key_2")
If numbered keys exist (e.g., "key_2"): increments the highest number
## Not run: # Basic usage seurat_obj <- AddMisc(seurat_obj, "QC_stats" = qc_df) # Auto-incrementing example seurat_obj <- AddMisc(seurat_obj, markers = markers1) seurat_obj <- AddMisc(seurat_obj, markers = markers2, cover=FALSE) # Stores as "markers" and "markers_1" ## End(Not run)## Not run: # Basic usage seurat_obj <- AddMisc(seurat_obj, "QC_stats" = qc_df) # Auto-incrementing example seurat_obj <- AddMisc(seurat_obj, markers = markers1) seurat_obj <- AddMisc(seurat_obj, markers = markers2, cover=FALSE) # Stores as "markers" and "markers_1" ## End(Not run)
A higher-order function that wraps CLI functions to automatically prepend timestamps to their output messages. This creates a modified version of any CLI function that includes timestamp information in its output.
AddTimeStamp2cli(cli_func)AddTimeStamp2cli(cli_func)
cli_func |
A CLI function from the |
This function uses force to ensure the CLI function is evaluated
at creation time. The timestamp is generated using a TimeStamp() function
which should be available in the execution environment and is inserted using
cli's glue-like syntax "{ }".
Returns a modified version of the input function that automatically
adds a timestamp in the format "[{timestamp}]" to the beginning
of all character messages passed to it.
CreateTimeStampCliEnv for creating a complete environment
of timestamped CLI functions.
Other TimeStamp:
CreateTimeStampCliEnv(),
TimeStamp()
## Not run: # Create a timestamp-enabled version of cli_alert_info timestamp_alert <- AddTimeStamp2cli(cli::cli_alert_info) timestamp_alert("This message will have a timestamp") ## End(Not run)## Not run: # Create a timestamp-enabled version of cli_alert_info timestamp_alert <- AddTimeStamp2cli(cli::cli_alert_info) timestamp_alert("This message will have a timestamp") ## End(Not run)
all_identical checks if all provided objects are pairwise identical and returns a symmetric matrix showing the comparison results between all object pairs.
all_identical(..., names = NULL)all_identical(..., names = NULL)
... |
Objects to compare for identity |
names |
Optional character vector of names for the objects. If not provided, objects will be named as "Obj1", "Obj2", etc. |
This function provides a comprehensive way to compare multiple objects at once, returning a matrix that shows all pairwise comparisons. This is particularly useful for testing and validation scenarios where you need to verify that multiple objects are identical.
A symmetric logical matrix where entry [i, j] indicates whether object i is identical to object j. The matrix has dimensions n x n where n is the number of objects, with row and column names from the names parameter.
identical() for pairwise object comparison,
matrix() for creating matrices
# Compare identical objects x <- 1:5 y <- 1:5 z <- 1:5 # All objects are identical result <- all_identical(x, y, z) print(result) # Compare different objects with custom names a <- 1:3 b <- 1:5 c <- 1:3 result2 <- all_identical(a, b, c, names = c("first", "second", "third")) print(result2) # Single object case single_result <- all_identical(x) print(single_result)# Compare identical objects x <- 1:5 y <- 1:5 z <- 1:5 # All objects are identical result <- all_identical(x, y, z) print(result) # Compare different objects with custom names a <- 1:3 b <- 1:5 c <- 1:3 result2 <- all_identical(a, b, c, names = c("first", "second", "third")) print(result2) # Single object case single_result <- all_identical(x) print(single_result)
Generates an environment containing CLI functions that automatically report their caller (Global or Function Name).
CreateCallerCliEnv( cli_functions = c("cli_alert_info", "cli_alert_success", "cli_alert_warning", "cli_alert_danger", "cli_inform") )CreateCallerCliEnv( cli_functions = c("cli_alert_info", "cli_alert_success", "cli_alert_warning", "cli_alert_danger", "cli_inform") )
cli_functions |
Character vector of function names from package |
An environment with wrapped functions.
## Not run: caller_cli <- CreateCallerCliEnv() # Global context caller_cli$cli_alert_info("Hello") # Output: [global]: Hello # Function context f <- function(x) { caller_cli$cli_alert_success("Result is {x}") } f(100) # Output: [f()]: Result is 100 ## End(Not run)## Not run: caller_cli <- CreateCallerCliEnv() # Global context caller_cli$cli_alert_info("Hello") # Output: [global]: Hello # Function context f <- function(x) { caller_cli$cli_alert_success("Result is {x}") } f(100) # Output: [f()]: Result is 100 ## End(Not run)
Generates an environment containing wrapped versions of common CLI functions that automatically include timestamps in their output. This provides a convenient way to use multiple CLI functions with consistent timestamping.
CreateTimeStampCliEnv( cli_functions = c("cli_alert_info", "cli_alert_success", "cli_alert_warning", "cli_alert_danger") )CreateTimeStampCliEnv( cli_functions = c("cli_alert_info", "cli_alert_success", "cli_alert_warning", "cli_alert_danger") )
cli_functions |
from package |
This function creates a new environment and populates it with timestamped
versions of commonly used CLI functions from the cli package. Only
functions that exist in the loaded cli package are added to the
environment. The function uses walk for side-effect
iteration over the function names.
Returns an environment containing timestamp-wrapped versions of:
cli_warn
cli_alert_info
cli_alert_success
cli_alert_warning
cli_alert_danger
Each function in the environment will automatically prepend timestamps to its output messages.
AddTimeStamp2cli for the wrapper function used internally.
Other TimeStamp:
AddTimeStamp2cli(),
TimeStamp()
## Not run: # Create environment with timestamped CLI functions cli_env <- CreateTimeStampCliEnv() # Use timestamped functions cli_env$cli_alert_info("System started") cli_env$cli_alert_success("Operation completed") ## End(Not run)## Not run: # Create environment with timestamped CLI functions cli_env <- CreateTimeStampCliEnv() # Use timestamped functions cli_env$cli_alert_info("System started") cli_env$cli_alert_success("Operation completed") ## End(Not run)
FilterArgs4Func filters a list of arguments to include only those that
match the formal arguments of a specified function, with optional support
for preserving additional arguments via the keep parameter.
This is useful for preparing argument lists for function calls, especially
when dealing with functions that have many optional parameters or when
passing arguments through multiple function layers.
FilterArgs4Func(args_list, fun, keep = NULL)FilterArgs4Func(args_list, fun, keep = NULL)
args_list |
A named list of arguments to filter |
fun |
The target function whose formal arguments will be used for filtering |
keep |
Character vector of argument names to preserve regardless of
whether they appear in |
This function is particularly useful in scenarios where you have a large
list of parameters and want to pass only the relevant ones to a specific
function while preserving certain arguments for downstream processing
(e.g., arguments consumed by nested ... parameters).
The keep parameter enables flexible argument forwarding patterns
common in wrapper functions and pipeline designs.
A filtered list containing:
Arguments from args_list that match formal parameters of fun
(excluding the "..." parameter)
Additional arguments specified in keep (if not NULL)
formals() for accessing function formal arguments,
do.call() for executing functions with argument lists,
names() for working with list names
## Not run: # Example function with specific parameters example_function <- function(a, b, c = 10, d = 20) { return(a + b + c + d) } # Create a list with both relevant and irrelevant arguments all_args <- list( a = 1, b = 2, c = 3, e = 4, # Not in function formals f = 5 # Not in function formals ) # Basic usage: filter to only include arguments matching function parameters filtered_args <- FilterArgs4Func(all_args, example_function) print(filtered_args) #> $a #> [1] 1 #> #> $b #> [1] 2 #> #> $c #> [1] 3 # Advanced usage: preserve additional arguments for downstream processing filtered_with_keep <- FilterArgs4Func(all_args, example_function, keep = c("e", "f")) print(filtered_with_keep) #> $a #> [1] 1 #> #> $b #> [1] 2 #> #> $c #> [1] 3 #> #> $e #> [1] 4 #> #> $f #> [1] 5 # Execute with filtered arguments result <- do.call(example_function, filtered_args) print(result) #> [1] 16 ## End(Not run)## Not run: # Example function with specific parameters example_function <- function(a, b, c = 10, d = 20) { return(a + b + c + d) } # Create a list with both relevant and irrelevant arguments all_args <- list( a = 1, b = 2, c = 3, e = 4, # Not in function formals f = 5 # Not in function formals ) # Basic usage: filter to only include arguments matching function parameters filtered_args <- FilterArgs4Func(all_args, example_function) print(filtered_args) #> $a #> [1] 1 #> #> $b #> [1] 2 #> #> $c #> [1] 3 # Advanced usage: preserve additional arguments for downstream processing filtered_with_keep <- FilterArgs4Func(all_args, example_function, keep = c("e", "f")) print(filtered_with_keep) #> $a #> [1] 1 #> #> $b #> [1] 2 #> #> $c #> [1] 3 #> #> $e #> [1] 4 #> #> $f #> [1] 5 # Execute with filtered arguments result <- do.call(example_function, filtered_args) print(result) #> [1] 16 ## End(Not run)
Retrieves the name of the function that called the current execution context. If called from the global environment, returns "global".
GetCallerInfo(offset = 2)GetCallerInfo(offset = 2)
offset |
Integer. The number of stack frames to go back. Defaults to 2 (skipping the GetCallerInfo frame and the Wrapper frame to find the User's function). |
Character string. E.g., "my_function()" or "global".
## Not run: f <- function() { GetCallerInfo() } f() # Returns "f()" ## End(Not run)## Not run: f <- function() { GetCallerInfo() } f() # Returns "f()" ## End(Not run)
This function attempts to find the Python executable within a given environment path. It checks different candidate paths based on the operating system.
GetPythonPath(path)GetPythonPath(path)
path |
Character string specifying the path to the environment where Python might be located. |
Character string with the normalized path to the Python executable if found,
otherwise NA_character_.
Compute the Moore-Penrose generalized inverse of a matrix. This is an S3 generic function with methods for base matrices, dense Matrix objects, and sparse Matrix objects.
Default method for base R matrices (from MASS::ginv)
ginv2(X, tol = sqrt(.Machine$double.eps), ...) ## Default S3 method: ginv2(X, tol = sqrt(.Machine$double.eps), ...)ginv2(X, tol = sqrt(.Machine$double.eps), ...) ## Default S3 method: ginv2(X, tol = sqrt(.Machine$double.eps), ...)
X |
A numeric or complex matrix |
tol |
Tolerance for determining rank. Default is sqrt(.Machine$double.eps) |
... |
Additional arguments passed to methods |
The generalized inverse of X
## Not run: # Base R matrix m <- matrix(c(1, 2, 3, 4, 5, 6), 2, 3) ginv2(m) # Dense Matrix library(Matrix) dm <- Matrix(m, sparse = FALSE) ginv2(dm) # Sparse Matrix sm <- Matrix(m, sparse = TRUE) ginv2(sm) ## End(Not run)## Not run: # Base R matrix m <- matrix(c(1, 2, 3, 4, 5, 6), 2, 3) ginv2(m) # Dense Matrix library(Matrix) dm <- Matrix(m, sparse = FALSE) ginv2(dm) # Sparse Matrix sm <- Matrix(m, sparse = TRUE) ginv2(sm) ## End(Not run)
Discovers and lists available Python environments of various types on the system. This generic function provides a unified interface to find Conda environments and virtual environments (venv) through S3 method dispatch.
Default method that lists all Python environments by combining results from Conda and virtual environment discovery methods.
Discovers Conda environments using multiple detection strategies for maximum reliability. First attempts to use system Conda commands, then falls back to reticulate's built-in Conda interface if Conda command is unavailable or fails. Returns empty data frame if Conda is not available or no environments are found.
Discovers virtual environments by searching common venv locations including
user directories (~/.virtualenvs, ~/.venvs) and project folders
(./venv, ./.venv). Supports custom search paths through the
venv_locations parameter. Returns empty data frame if no virtual
environments are found in the specified locations.
ListPyEnv( env_type = c("all", "conda", "venv", "virtualenv"), timeout = 30000L, venv_locations = c("~/.virtualenvs", "~/.venvs", "./venv", "./.venv"), verbose = TRUE, ... ) ## Default S3 method: ListPyEnv( env_type = c("all", "conda", "venv", "virtualenv"), timeout = 30000L, venv_locations = c("~/.virtualenvs", "~/.venvs", "./venv", "./.venv"), verbose = getFuncOption("verbose") %||% TRUE, ... ) ## S3 method for class 'conda' ListPyEnv( env_type = c("all", "conda", "venv", "virtualenv"), timeout = 30000L, venv_locations = c("~/.virtualenvs", "~/.venvs", "./venv", "./.venv"), verbose = getFuncOption("verbose") %||% TRUE, ... ) ## S3 method for class 'venv' ListPyEnv( env_type = c("all", "conda", "venv", "virtualenv"), timeout = 30000L, venv_locations = c("~/.virtualenvs", "~/.venvs", "./venv", "./.venv"), verbose = getFuncOption("verbose") %||% TRUE, ... )ListPyEnv( env_type = c("all", "conda", "venv", "virtualenv"), timeout = 30000L, venv_locations = c("~/.virtualenvs", "~/.venvs", "./venv", "./.venv"), verbose = TRUE, ... ) ## Default S3 method: ListPyEnv( env_type = c("all", "conda", "venv", "virtualenv"), timeout = 30000L, venv_locations = c("~/.virtualenvs", "~/.venvs", "./venv", "./.venv"), verbose = getFuncOption("verbose") %||% TRUE, ... ) ## S3 method for class 'conda' ListPyEnv( env_type = c("all", "conda", "venv", "virtualenv"), timeout = 30000L, venv_locations = c("~/.virtualenvs", "~/.venvs", "./venv", "./.venv"), verbose = getFuncOption("verbose") %||% TRUE, ... ) ## S3 method for class 'venv' ListPyEnv( env_type = c("all", "conda", "venv", "virtualenv"), timeout = 30000L, venv_locations = c("~/.virtualenvs", "~/.venvs", "./venv", "./.venv"), verbose = getFuncOption("verbose") %||% TRUE, ... )
env_type |
Character string specifying the type of environments to list.
One of: |
timeout |
The maximum timeout time when using system commands, only effective when |
venv_locations |
Character vector of directory paths to search for virtual environments. Default includes standard locations and common project directories. |
verbose |
Logical indicating whether to print verbose output. |
... |
For future use. |
The function uses S3 method dispatch to handle different environment types:
"all": Combines results from all environment types using rbind()
"conda": Searches for Conda environments using multiple methods:
Primary: reticulate::conda_list() for reliable environment detection
Fallback: System conda info --envs command for broader compatibility
"venv": Searches common virtual environment locations including
user directories and project folders
Each method includes comprehensive error handling and will return empty results with informative warnings if no environments are found or if errors occur during discovery.
A data frame with the following columns:
name - Character vector of environment names
python - Character vector of paths to Python executables
type - Character vector indicating environment type ("conda" or "venv")
Returns an empty data frame with these columns if no environments are found.
## Not run: # List all Python environments ListPyEnv("all") # List only Conda environments ListPyEnv("conda") # List only virtual environments with custom search paths ListPyEnv("venv", venv_locations = c("~/my_envs", "./project_env")) ## End(Not run)## Not run: # List all Python environments ListPyEnv("all") # List only Conda environments ListPyEnv("conda") # List only virtual environments with custom search paths ListPyEnv("venv", venv_locations = c("~/my_envs", "./project_env")) ## End(Not run)
A robust argument matching function that supports exact matching, partial matching, and provides sensible defaults when no match is found. This is an internal utility function not intended for direct use by package users.
MatchArg(arg, choices, default = choices[1], call = rlang::caller_env(), ...)MatchArg(arg, choices, default = choices[1], call = rlang::caller_env(), ...)
arg |
The argument to match against choices. Can be |
choices |
A character vector of valid choices to match against. |
default |
The default value to return if no match is found and |
call |
caller env |
... |
No usage |
This function provides a more flexible alternative to base::match.arg() with
the following matching strategy:
If arg is NULL, returns the default value
Attempts exact matching using base::match()
Falls back to partial matching using base::pmatch()
If no match found and default is not NULL, returns default
Otherwise, throws an informative error with valid choices
The function uses rlang::caller_env() for accurate error reporting in the
context where the function was called.
Returns the matched choice from the choices vector. If no match is found
and arg is NULL, returns the default value. If no match is found and
arg is not NULL, throws an informative error.
match for exact matching \
pmatch for partial matching \
caller_env for calling environment context
## Not run: # Internal usage examples MatchArg("app", c("apple", "banana", "application")) # Returns "apple" MatchArg(NULL, c("red", "green", "blue")) # Returns "red" (default) MatchArg("gr", c("red", "green", "blue")) # Returns "green" # Would error: MatchArg("invalid", c("valid1", "valid2")) ## End(Not run)## Not run: # Internal usage examples MatchArg("app", c("apple", "banana", "application")) # Returns "apple" MatchArg(NULL, c("red", "green", "blue")) # Returns "red" (default) MatchArg("gr", c("red", "green", "blue")) # Returns "green" # Would error: MatchArg("invalid", c("valid1", "valid2")) ## End(Not run)
Identifies functions compatible with a given set of named arguments. A function is considered compatible if:
It has a ... parameter (loose matching), OR
All argument names in args_list exist in its formal parameters
(strict matching, default behavior).
MatchFunc2Args( args_list, ..., name_only = FALSE, top_one_only = FALSE, dots_enabled = FALSE )MatchFunc2Args( args_list, ..., name_only = FALSE, top_one_only = FALSE, dots_enabled = FALSE )
args_list |
Named list of arguments to match. Must have non-empty names when non-empty. |
... |
Functions to test for compatibility. |
name_only |
Logical. If |
top_one_only |
Logical. If |
dots_enabled |
Logical. If |
name_only = FALSE, top_one_only = FALSE: List of compatible function objects
name_only = TRUE, top_one_only = FALSE: Character vector of function identifiers
(named functions retain their symbol name; anonymous functions become "anonymous_<index>")
top_one_only = TRUE: Single function object or name (depending on name_only)
FilterArgs4Func() for filtering arguments to a function.(Reverse of this function)
## Not run: f1 <- function(a, b) a + b f2 <- function(x, y, ...) x * y f3 <- function(p, q) p - q args <- list(a = 1, b = 2) # Strict matching (default): returns f1 only MatchFunc2Args(args, f1, f2, f3) # Loose matching: returns f1 and f2 (both accept 'a' and 'b' via strict match or ...) MatchFunc2Args(args, f1, f2, f3, dots_enabled = TRUE) # Return only function names MatchFunc2Args(args, f1, f2, name_only = TRUE) # Returns: c("f1", "f2") when dots_enabled=TRUE ## End(Not run)## Not run: f1 <- function(a, b) a + b f2 <- function(x, y, ...) x * y f3 <- function(p, q) p - q args <- list(a = 1, b = 2) # Strict matching (default): returns f1 only MatchFunc2Args(args, f1, f2, f3) # Loose matching: returns f1 and f2 (both accept 'a' and 'b' via strict match or ...) MatchFunc2Args(args, f1, f2, f3, dots_enabled = TRUE) # Return only function names MatchFunc2Args(args, f1, f2, name_only = TRUE) # Returns: c("f1", "f2") when dots_enabled=TRUE ## End(Not run)
A collection of functions for computing matrix statistics with fallback implementations when specialized packages are not available.
sparseMatrixStats -> sparseMatrix
matrixStats -> matrix
Matrix -> denseMatrix
base R -> matrix
normalize.quantiles performs quantile normalization on a matrix, transforming the distributions of each column to match a common target distribution. Uses preprocessCore::normalize.quantiles if available, otherwise provides a pure R implementation.
rowMeans3(x, na.rm = FALSE, ...) colMeans3(x, na.rm = FALSE, ...) rowVars3(x, na.rm = FALSE, ...) colVars3(x, na.rm = FALSE, ...) rowSds3(x, na.rm = FALSE, ...) colSds3(x, na.rm = FALSE, ...) colQuantiles3(x, probs = seq(0, 1, 0.25), ...) rowMaxs3( x, rows = NULL, cols = NULL, na.rm = FALSE, dim. = dim(x), ..., useNames = TRUE ) colSums3(x, na.rm = FALSE, ...) rowSums3(x, na.rm = FALSE, ...) rowMedians3( x, rows = NULL, cols = NULL, na.rm = FALSE, dim. = dim(x), ..., useNames = TRUE ) colMedians3( x, rows = NULL, cols = NULL, na.rm = FALSE, dim. = dim(x), ..., useNames = TRUE ) normalize.quantiles(x, copy = TRUE, keep.names = FALSE, ...)rowMeans3(x, na.rm = FALSE, ...) colMeans3(x, na.rm = FALSE, ...) rowVars3(x, na.rm = FALSE, ...) colVars3(x, na.rm = FALSE, ...) rowSds3(x, na.rm = FALSE, ...) colSds3(x, na.rm = FALSE, ...) colQuantiles3(x, probs = seq(0, 1, 0.25), ...) rowMaxs3( x, rows = NULL, cols = NULL, na.rm = FALSE, dim. = dim(x), ..., useNames = TRUE ) colSums3(x, na.rm = FALSE, ...) rowSums3(x, na.rm = FALSE, ...) rowMedians3( x, rows = NULL, cols = NULL, na.rm = FALSE, dim. = dim(x), ..., useNames = TRUE ) colMedians3( x, rows = NULL, cols = NULL, na.rm = FALSE, dim. = dim(x), ..., useNames = TRUE ) normalize.quantiles(x, copy = TRUE, keep.names = FALSE, ...)
x |
A numeric matrix where columns represent samples and rows represent features. |
na.rm |
Logical indicating whether to remove missing values |
... |
Additional arguments (currently not used). |
probs |
Numeric vector of probabilities with values between 0 and 1. |
rows, cols
|
Indices specifying subset of rows/columns to operate over |
dim. |
Dimensions of the input matrix |
useNames |
Logical indicating whether to preserve row names in output |
copy |
Logical indicating whether to work on a copy of the matrix (TRUE) or modify in-place (FALSE). |
keep.names |
Logical indicating whether to preserve row and column names. |
A numeric vector of length nrow(x) containing row variances.
A numeric vector of length nrow(x) containing row variances.
A numeric vector of length nrow(x) containing row variances.
A numeric vector of length ncol(x) containing column variances.
A numeric vector of length nrow(x) containing row standard deviations.
A numeric vector of length ncol(x) containing column standard deviations.
A matrix of quantiles with length(probs) rows and ncol(x) columns.
A numeric vector of length nrow(x) containing row maximums
A numeric vector of length ncol(x) containing column sums.
A numeric vector of length nrow(x) containing row sums.
A numeric vector of length nrow(x) containing row sums.
A numeric vector of length nrow(x) containing row sums.
A numeric matrix of the same dimensions as x with quantile-normalized data.
matrixStats::rowVars() for the underlying implementation
matrixStats::colSds() for the underlying implementation
preprocessCore::normalize.quantiles() for the underlying implementation
mat <- matrix(rnorm(100), nrow = 10, ncol = 10) # Compute row variances row_vars <- rowVars3(mat) # With missing values mat[1, 1] <- NA row_vars_na <- rowVars3(mat, na.rm = TRUE) mat <- matrix(rnorm(100), nrow = 10, ncol = 10) # Compute column variances col_vars <- colVars3(mat) # With missing values mat[1, 1] <- NA col_vars_na <- colVars3(mat, na.rm = TRUE) mat <- matrix(rnorm(100), nrow = 10, ncol = 10) row_sds <- rowSds3(mat) mat <- matrix(rnorm(100), nrow = 10, ncol = 10) col_sds <- colSds3(mat) mat <- matrix(rnorm(100), nrow = 10, ncol = 10) # Compute quartiles for each column quartiles <- colQuantiles3(mat) # Compute specific quantiles specific_quantiles <- colQuantiles3(mat, probs = c(0.1, 0.5, 0.9)) mat <- matrix(rnorm(100), nrow = 10, ncol = 10) # Compute row maximums row_maxs <- rowMaxs3(mat) # With missing values mat[1, 1] <- NA row_maxs_na <- rowMaxs3(mat, na.rm = TRUE) mat <- matrix(rnorm(100), nrow = 10, ncol = 10) # Perform quantile normalization normalized_mat <- normalize.quantiles(mat) # Preserve original names rownames(mat) <- paste0("Gene", 1:10) colnames(mat) <- paste0("Sample", 1:10) normalized_with_names <- normalize.quantiles(mat, keep.names = TRUE)mat <- matrix(rnorm(100), nrow = 10, ncol = 10) # Compute row variances row_vars <- rowVars3(mat) # With missing values mat[1, 1] <- NA row_vars_na <- rowVars3(mat, na.rm = TRUE) mat <- matrix(rnorm(100), nrow = 10, ncol = 10) # Compute column variances col_vars <- colVars3(mat) # With missing values mat[1, 1] <- NA col_vars_na <- colVars3(mat, na.rm = TRUE) mat <- matrix(rnorm(100), nrow = 10, ncol = 10) row_sds <- rowSds3(mat) mat <- matrix(rnorm(100), nrow = 10, ncol = 10) col_sds <- colSds3(mat) mat <- matrix(rnorm(100), nrow = 10, ncol = 10) # Compute quartiles for each column quartiles <- colQuantiles3(mat) # Compute specific quantiles specific_quantiles <- colQuantiles3(mat, probs = c(0.1, 0.5, 0.9)) mat <- matrix(rnorm(100), nrow = 10, ncol = 10) # Compute row maximums row_maxs <- rowMaxs3(mat) # With missing values mat[1, 1] <- NA row_maxs_na <- rowMaxs3(mat, na.rm = TRUE) mat <- matrix(rnorm(100), nrow = 10, ncol = 10) # Perform quantile normalization normalized_mat <- normalize.quantiles(mat) # Preserve original names rownames(mat) <- paste0("Gene", 1:10) colnames(mat) <- paste0("Sample", 1:10) normalized_with_names <- normalize.quantiles(mat, keep.names = TRUE)
The %||% operator provides a convenient way to handle NULL values by returning a default value when the left-hand side is NULL. This is particularly useful for providing fallback values in function arguments and data processing.
lhs %||% rhslhs %||% rhs
lhs |
Left-hand side value to check for NULL |
rhs |
Right-hand side value to return if lhs is NULL |
This operator follows the same semantics as the null coalescing operator found in other programming languages (e.g., ?? in C#, ?: in JavaScript). It provides a concise way to specify default values without verbose if-else statements.
Returns lhs if it is not NULL, otherwise returns rhs.
is.null() for checking NULL values,
ifelse() for more complex conditional logic
## Not run: # Basic usage with NULL values NULL %||% "default value" # Returns "default value" "actual value" %||% "default value" # Returns "actual value" # Practical use in functions my_function <- function(x = NULL) { x <- x %||% "default_parameter" print(x) } my_function() # Prints "default_parameter" my_function("custom_value") # Prints "custom_value" # Handling potentially NULL results result <- tryCatch( some_operation(), error = function(e) NULL ) final_value <- result %||% "fallback" ## End(Not run)## Not run: # Basic usage with NULL values NULL %||% "default value" # Returns "default value" "actual value" %||% "default value" # Returns "actual value" # Practical use in functions my_function <- function(x = NULL) { x <- x %||% "default_parameter" print(x) } my_function() # Prints "default_parameter" my_function("custom_value") # Prints "custom_value" # Handling potentially NULL results result <- tryCatch( some_operation(), error = function(e) NULL ) final_value <- result %||% "fallback" ## End(Not run)
Functions for converting between column-based row names and explicit row name attributes. These utilities provide convenient ways to handle row names in data frames and matrices, serving as alternatives to tibble's row name handling functions.
Col2Rownames converts a specified column to row names and removes the original column. This is useful when working with data that has identifier columns that should serve as row names rather than regular data columns.
Rownames2Col converts row names to an explicit column and removes the row names attribute. This is useful when preparing data for functions that don't handle row names well, or when row names need to be included as regular data for analysis or visualization.
Col2Rownames(.data, var = "rowname") Rownames2Col(.data, var = "rowname")Col2Rownames(.data, var = "rowname") Rownames2Col(.data, var = "rowname")
.data |
A data frame or matrix-like object with row names |
var |
Character string specifying the name for the new column that will contain the row names. Defaults to "rowname". |
The input object with row names set from the specified column and that column removed from the data.
The input object with row names converted to a new column and the row names attribute set to NULL.
# Create sample data with an ID column df <- data.frame( gene_id = paste0("GENE", 1:5), expression = rnorm(5), p_value = runif(5) ) # Convert gene_id column to row names df_with_rownames <- Col2Rownames(df, var = "gene_id") print(rownames(df_with_rownames)) # Create sample data with row names df <- data.frame( expression = rnorm(5), p_value = runif(5) ) rownames(df) <- paste0("GENE", 1:5) # Convert row names to explicit column df_with_col <- Rownames2Col(df, var = "gene_id") print(df_with_col$gene_id)# Create sample data with an ID column df <- data.frame( gene_id = paste0("GENE", 1:5), expression = rnorm(5), p_value = runif(5) ) # Convert gene_id column to row names df_with_rownames <- Col2Rownames(df, var = "gene_id") print(rownames(df_with_rownames)) # Create sample data with row names df <- data.frame( expression = rnorm(5), p_value = runif(5) ) rownames(df) <- paste0("GENE", 1:5) # Convert row names to explicit column df_with_col <- Rownames2Col(df, var = "gene_id") print(df_with_col$gene_id)
Sets up a Python environment with specified packages. This function can create new environments or reuse existing ones, supporting both Conda and venv environment types. It ensures all required dependencies are properly installed and verified.
Default method for unsupported environment types. Throws an informative error with supported environment types.
SetupPyEnv(env_type = c("conda", "venv"), ...) ## Default S3 method: SetupPyEnv(env_type = c("conda", "venv"), ...)SetupPyEnv(env_type = c("conda", "venv"), ...) ## Default S3 method: SetupPyEnv(env_type = c("conda", "venv"), ...)
env_type |
Character string specifying the type of Python environment to
create or use. One of: |
... |
Additional parameters passed to specific environment methods. |
This function provides a comprehensive solution for Python environment management in R projects, particularly for machine learning workflows requiring TensorFlow. Key features include:
Environment Creation: Automatically creates new environments or reuses existing ones with the same name
Package Management: Installs specified Python packages with version pinning support
Verification: Validates environment setup and package installations
Flexible Methods: Supports different backend methods for environment creation (reticulate vs system calls)
The function uses S3 method dispatch to handle different environment types, allowing for extensible support of additional environment managers in the future.
A data frame containing verification results for the environment setup, including installation status of all required packages. Invisibly returns the verification results.
reticulate::conda_create(), reticulate::virtualenv_create() for
underlying environment creation functions.
## Not run: # Setup a Conda environment with default parameters SetupPyEnv("conda") # Setup a venv environment SetupPyEnv("venv") ## End(Not run)## Not run: # Setup a Conda environment with default parameters SetupPyEnv("conda") # Setup a venv environment SetupPyEnv("venv") ## End(Not run)
Creates and configures a Conda environment specifically designed for screening workflows. This function provides multiple methods for environment creation and package installation, including support for environment files, with comprehensive verification and error handling.
## S3 method for class 'conda' SetupPyEnv( env_type = "conda", env_name = "r-reticulate-degas", method = c("reticulate", "system", "environment"), env_file = NULL, python_version = "3.9.15", packages = c(tensorflow = "2.4.1", protobuf = "3.20.3"), recreate = FALSE, use_conda_forge = TRUE, ... )## S3 method for class 'conda' SetupPyEnv( env_type = "conda", env_name = "r-reticulate-degas", method = c("reticulate", "system", "environment"), env_file = NULL, python_version = "3.9.15", packages = c(tensorflow = "2.4.1", protobuf = "3.20.3"), recreate = FALSE, use_conda_forge = TRUE, ... )
env_type |
Character string specifying the environment type. For this method, must be "conda". |
env_name |
Character string specifying the Conda environment name. Default: "r-reticulate-degas". |
method |
Character string specifying the method for environment creation and package installation. One of: "reticulate" (uses reticulate package), "system" (uses system conda commands), or "environment" (uses YAML environment file). Default: "reticulate". |
env_file |
Character string specifying the path to a Conda environment YAML file. Used when method = "environment". Default: NULL |
python_version |
Character string specifying the Python version to install. Default: "3.9.15". |
packages |
Named character vector of Python packages to install. Package names as names, versions as values. Use "any" for version to install latest available. Default includes tensorflow, protobuf, and numpy. |
recreate |
Logical indicating whether to force recreation of the environment if it already exists. Default: FALSE. |
use_conda_forge |
Logical indicating whether to use the conda-forge channel for package installation. Default: TRUE. |
... |
Additional arguments. Currently supports:
|
Invisibly returns NULL.
The function requires Conda to be installed and accessible on the system PATH or through reticulate. For method = "environment", the specified YAML file must exist and be properly formatted. The function includes extensive error handling but may fail if Conda is not properly configured.
reticulate::conda_create(), reticulate::py_install() for the underlying
functions used in reticulate method.
## Not run: # Setup using reticulate method (default) SetupPyEnv.conda( env_name = "my-degas-env", python_version = "3.9.15" ) # Setup using environment file SetupPyEnv.conda( method = "environment", env_file = "path/to/environment.yml" ) # Setup with custom packages SetupPyEnv.conda( packages = c( "tensorflow" = "2.4.1", "scikit-learn" = "1.0.2", "pandas" = "any" ) ) ## End(Not run)## Not run: # Setup using reticulate method (default) SetupPyEnv.conda( env_name = "my-degas-env", python_version = "3.9.15" ) # Setup using environment file SetupPyEnv.conda( method = "environment", env_file = "path/to/environment.yml" ) # Setup with custom packages SetupPyEnv.conda( packages = c( "tensorflow" = "2.4.1", "scikit-learn" = "1.0.2", "pandas" = "any" ) ) ## End(Not run)
Creates and configures a Python virtual environment (venv) specifically designed for screening workflows. This function provides a lightweight, isolated Python environment alternative to Conda environments with similar package management capabilities.
## S3 method for class 'venv' SetupPyEnv( env_type = "venv", env_name = "r-reticulate-degas", python_version = "3.9.15", packages = c(tensorflow = "2.4.1", protobuf = "3.20.3"), python_path = NULL, recreate = FALSE, ... )## S3 method for class 'venv' SetupPyEnv( env_type = "venv", env_name = "r-reticulate-degas", python_version = "3.9.15", packages = c(tensorflow = "2.4.1", protobuf = "3.20.3"), python_path = NULL, recreate = FALSE, ... )
env_type |
Character string specifying the environment type. For this method, must be "venv". |
env_name |
Character string specifying the virtual environment name. Default: "r-reticulate-degas". |
python_version |
Character string specifying the Python version to use. Default: "3.9.15". |
packages |
Named character vector of Python packages to install. Package names as names, versions as values. Use "any" for version to install latest available. Default includes tensorflow, protobuf, and numpy. |
python_path |
Character string specifying the path to a specific Python executable. If NULL, uses the system default or installs the specified version. Default: NULL. |
recreate |
Logical indicating whether to force recreation of the virtual environment if it already exists. Default: FALSE. |
... |
Additional arguments. Currently supports:
|
Invisibly returns NULL.
Virtual environments require a base Python installation. If the specified Python version is not available, the function will attempt to install it using reticulate. Virtual environments are generally faster to create than Conda environments but may have more limited package availability compared to Conda-forge.
reticulate::virtualenv_create(), reticulate::virtualenv_remove(),
reticulate::use_virtualenv() for the underlying virtual environment
management functions.
## Not run: # Setup virtual environment with default parameters SetupPyEnv.venv() # Setup with custom Python version and packages SetupPyEnv.venv( env_name = "my-degas-venv", python_version = "3.8.12", packages = c( "tensorflow" = "2.4.1", "scikit-learn" = "1.0.2", "pandas" = "any" ) ) # Force recreate existing environment SetupPyEnv.venv( env_name = "existing-env", recreate = TRUE ) ## End(Not run)## Not run: # Setup virtual environment with default parameters SetupPyEnv.venv() # Setup with custom Python version and packages SetupPyEnv.venv( env_name = "my-degas-venv", python_version = "3.8.12", packages = c( "tensorflow" = "2.4.1", "scikit-learn" = "1.0.2", "pandas" = "any" ) ) # Force recreate existing environment SetupPyEnv.venv( env_name = "existing-env", recreate = TRUE ) ## End(Not run)
These functions provide a centralized configuration system for the SigBridgeR package, allowing users to set and retrieve package-specific options with automatic naming conventions.
setFunOption sets one or more configuration options for the SigBridgeR package. Options are automatically prefixed with "SigBridgeR." if not already present, ensuring proper namespace isolation.
getFuncOption retrieves configuration options for the SigBridgeR package. The function automatically handles the "SigBridgeR." prefix, allowing users to reference options with or without the explicit prefix.
checkFuncOption validates configuration options for the SigBridgeR package. This function ensures that all options meet type and value requirements before they are set in the global options.
setFuncOption(...) getFuncOption(option = NULL, default = NULL) checkFuncOption(option, value, call = rlang::caller_env())setFuncOption(...) getFuncOption(option = NULL, default = NULL) checkFuncOption(option, value, call = rlang::caller_env())
... |
Named arguments representing option-value pairs. Options can be provided with or without the "SigBridgeR." prefix. If the prefix is missing, it will be automatically added. |
option |
Character string specifying the option name to check |
default |
The default value to return if the option is not set. Defaults to NULL. |
value |
The proposed value to assign to the option |
call |
The execution environment of a currently running function |
This function performs the following validations:
Must be single logical values (TRUE/FALSE)
Must be single integer values
The function is automatically called by setFuncOption to ensure all configuration options are valid before they are set.
Invisibly returns NULL. The function is called for its side effects of setting package options.
The value of the specified option if it exists, otherwise the provided default value.
No return value. The function throws an error if the value doesn't meet the required specifications for the given option.
verbose: A logical value indicating whether to print verbose messages, defaults to TRUE
timeout: An integer specifying the timeout in seconds for parallel processing, defaults to '180L“
seed: An integer specifying the random seed for reproducible results, defaults to '123L“
# Set options with automatic prefixing setFuncOption(verbose = TRUE) # Retrieve options with automatic prefixing getFuncOption("verbose")# Set options with automatic prefixing setFuncOption(verbose = TRUE) # Retrieve options with automatic prefixing getFuncOption("verbose")
Creates a formatted character string representing the current system time. The format is "YYYY/mm/DD HH:MM:SS" (year/month/day hour:minute:second).
TimeStamp()TimeStamp()
Character string with current time in "YYYY/mm/DD HH:MM:SS" format. If system time is unavailable, returns a fixed timestamp.
Other TimeStamp:
AddTimeStamp2cli(),
CreateTimeStampCliEnv()
## Not run: # Current time as formatted string TimeStamp() # Returns something like: "2025/06/15 16:04:00" ## End(Not run)## Not run: # Current time as formatted string TimeStamp() # Returns something like: "2025/06/15 16:04:00" ## End(Not run)