| Title: | Supporting Functions for Packages Maintained by 'YuLab-SMU' |
|---|---|
| Description: | Miscellaneous functions commonly used by 'YuLab-SMU'. |
| Authors: | Guangchuang Yu [aut, cre] (ORCID: <https://orcid.org/0000-0002-6485-8781>) |
| Maintainer: | Guangchuang Yu <[email protected]> |
| License: | Artistic-2.0 |
| Version: | 0.2.4.001 |
| Built: | 2026-05-24 07:20:34 UTC |
| Source: | https://github.com/yulab-smu/yulab.utils |
Convert a numeric or character vector to a chunked_array object. If the input is already a chunked_array, it is returned as-is.
as_chunked_array(x)as_chunked_array(x)
x |
a vector or chunked_array object |
A chunked_array object
Concatenate vectors into a chunked array
c2(x, y)c2(x, y)
x |
a vector or chunked_array object |
y |
a vector or chunked_array object |
Combine two vector or chunked_array objects into a single chunked_array without copying the underlying data.
A chunked_array object
Guangchuang Yu
a <- c2(1:100, 101:200) length(a) a[150]a <- c2(1:100, 101:200) length(a) a[150]
Validates directory existence and accessibility with options to create if missing
check_directory( path, create_if_missing = FALSE, check_write_permission = TRUE, arg_name = "directory" )check_directory( path, create_if_missing = FALSE, check_write_permission = TRUE, arg_name = "directory" )
path |
Directory path |
create_if_missing |
Whether to create directory if it doesn't exist |
check_write_permission |
Whether to verify write permissions |
arg_name |
Name of the argument for error messages |
Invisible TRUE if valid, throws error otherwise
Other validate-utils:
check_file(),
check_input(),
check_packages(),
check_range()
Enhanced file validation with comprehensive checks and better error messages
check_file(path, operation = "read", must_exist = TRUE)check_file(path, operation = "read", must_exist = TRUE)
path |
File path |
operation |
Operation being performed (read, write, etc.) |
must_exist |
Whether the file must exist |
Invisible TRUE if operation can proceed, throws error otherwise
Other validate-utils:
check_directory(),
check_input(),
check_packages(),
check_range()
Enhanced input validation supporting base types and class checks.
check_input( x, type = NULL, length = NULL, min_length = NULL, max_length = NULL, allow_null = FALSE, arg_name = "input" )check_input( x, type = NULL, length = NULL, min_length = NULL, max_length = NULL, allow_null = FALSE, arg_name = "input" )
x |
Object to check |
type |
Expected type (e.g., |
length |
Expected length |
min_length |
Minimum length |
max_length |
Maximum length |
allow_null |
Whether |
arg_name |
Argument name for messages |
Invisible TRUE on success
Other validate-utils:
check_directory(),
check_file(),
check_packages(),
check_range()
Enhanced package checking with better error messages and validation
check_packages(packages, reason = "for this functionality") check_pkg(packages, reason = "for this functionality")check_packages(packages, reason = "for this functionality") check_pkg(packages, reason = "for this functionality")
packages |
Character vector of package names |
reason |
Reason why these packages are needed |
Invisible TRUE if all packages are available, throws error otherwise
Other validate-utils:
check_directory(),
check_file(),
check_input(),
check_range()
Validates that a numeric value falls within the specified range
check_range(x, min = NULL, max = NULL, inclusive = TRUE, arg_name = "value")check_range(x, min = NULL, max = NULL, inclusive = TRUE, arg_name = "value")
x |
Numeric value to check |
min |
Minimum allowed value (optional) |
max |
Maximum allowed value (optional) |
inclusive |
Whether bounds are inclusive (default: TRUE) |
arg_name |
Name of the argument for error messages |
Invisible TRUE if valid, throws error otherwise
Other validate-utils:
check_directory(),
check_file(),
check_input(),
check_packages()
all possible combinations of n sets
combinations(n)combinations(n)
n |
number of sets |
a list of all combinations
Markdown link to CRAN/Bioconductor
CRANpkg(pkg) Biocpkg(pkg)CRANpkg(pkg) Biocpkg(pkg)
pkg |
package name |
md text string
Guangchuang Yu
Other pkg-utils:
Githubpkg(),
get_dependencies(),
get_fun_from_pkg(),
is.installed(),
mypkg(),
packageTitle(),
pload()
Process YuLab File Download
download_yulab_file(destfile, urls, gzfile = FALSE, appname = NULL)download_yulab_file(destfile, urls, gzfile = FALSE, appname = NULL)
destfile |
character. Local file path. |
urls |
character vector. Base URLs for download. Remote is is |
gzfile |
logical. Whether the remote file is gzipped. |
appname |
character. R package name. |
Guangchuang Yu
run system command
exec(command)exec(command)
command |
system command to run |
An exec instance that stores system command outputs
Guangchuang Yu
Other os-utils:
has_internet(),
user_dir()
Get reverse dependencies
get_dependencies(pkg, repo = c("CRAN", "BioC"))get_dependencies(pkg, repo = c("CRAN", "BioC"))
pkg |
package name |
repo |
'CRAN' and/or 'BioC' |
reverse dependencies
Guangchuang Yu
Other pkg-utils:
CRANpkg(),
Githubpkg(),
get_fun_from_pkg(),
is.installed(),
mypkg(),
packageTitle(),
pload()
load function from package
get_fun_from_pkg(pkg, fun)get_fun_from_pkg(pkg, fun)
pkg |
package |
fun |
function |
function
Guangchuang Yu
Other pkg-utils:
CRANpkg(),
Githubpkg(),
get_dependencies(),
is.installed(),
mypkg(),
packageTitle(),
pload()
get_fun_from_pkg('utils', 'zip')get_fun_from_pkg('utils', 'zip')
Markdown link to GitHub
Githubpkg(user, pkg)Githubpkg(user, pkg)
user |
github user |
pkg |
package name |
md text string
Guangchuang Yu
Other pkg-utils:
CRANpkg(),
get_dependencies(),
get_fun_from_pkg(),
is.installed(),
mypkg(),
packageTitle(),
pload()
test for internect connection via reading lines from a URL
has_internet(site = "https://www.baidu.com/")has_internet(site = "https://www.baidu.com/")
site |
URL to test connection |
logical value
Guangchuang Yu
Other os-utils:
exec(),
user_dir()
Utilities to cache intermediate data: initialize items, update items, remove items, and retrieve elements.
initial_cache() get_cache() rm_cache() initial_cache_item(item) get_cache_item(item) rm_cache_item(item) update_cache_item(item, elements, ttl = NULL) get_cache_element(item, elements, default = NULL, prune_expired = TRUE) prune_cache_item(item) cache_list_items() cache_size() cache_save(path) cache_load(path) with_cache(item, key, compute, ttl = NULL)initial_cache() get_cache() rm_cache() initial_cache_item(item) get_cache_item(item) rm_cache_item(item) update_cache_item(item, elements, ttl = NULL) get_cache_element(item, elements, default = NULL, prune_expired = TRUE) prune_cache_item(item) cache_list_items() cache_size() cache_save(path) cache_load(path) with_cache(item, key, compute, ttl = NULL)
item |
Cache item name |
elements |
Elements to cache |
ttl |
Time-to-live in seconds |
default |
Default value if cache element is missing |
prune_expired |
Logical, whether to prune expired items |
path |
File path to save or load cache |
key |
Element key |
compute |
Function to compute value when missing |
Cache environment, item, or selected elements
## Not run: slow_fib <- function(x) { if (x < 2) return(1) slow_fib(x-2) + slow_fib(x-1) } fast_fib <- function(x) { if (x < 2) return(1) res <- get_cache_element('fibonacci', as.character(x)) if (!is.null(res)) { return(res) } res <- fast_fib(x-2) + fast_fib(x-1) e <- list() e[[as.character(x)]] <- res update_cache_item('fibonacci', e) return(res) } system.time(slow_fib(30)) system.time(fast_fib(30)) ## End(Not run)## Not run: slow_fib <- function(x) { if (x < 2) return(1) slow_fib(x-2) + slow_fib(x-1) } fast_fib <- function(x) { if (x < 2) return(1) res <- get_cache_element('fibonacci', as.character(x)) if (!is.null(res)) { return(res) } res <- fast_fib(x-2) + fast_fib(x-1) e <- list() e[[as.character(x)]] <- res update_cache_item('fibonacci', e) return(res) } system.time(slow_fib(30)) system.time(fast_fib(30)) ## End(Not run)
install R package from zip file of source codes
install_zip(file, subdir = NULL, args = "--no-build-vignettes")install_zip(file, subdir = NULL, args = "--no-build-vignettes")
file |
zip file |
subdir |
sub directory that contains R package files, default is NULL |
args |
argument to build package |
No return value, called for install R package from zip file of source codes
Guangchuang Yu
install github package
install_zip_gh( repo, ref = "HEAD", subdir = NULL, args = "--no-build-vignettes" )install_zip_gh( repo, ref = "HEAD", subdir = NULL, args = "--no-build-vignettes" )
repo |
github repo |
ref |
github branch, default is HEAD, which means the default branch of the GitHub repo |
subdir |
sub directory that contains R package files, default is NULL |
args |
argument to build package |
it download the zip file first and use install_zip to install it
No return value, called for installing github package
Guangchuang Yu
Check whether packages are installed
is.installed(packages)is.installed(packages)
packages |
package names |
logical vector
Guangchuang Yu
Other pkg-utils:
CRANpkg(),
Githubpkg(),
get_dependencies(),
get_fun_from_pkg(),
mypkg(),
packageTitle(),
pload()
is.installed(c("dplyr", "ggplot2"))is.installed(c("dplyr", "ggplot2"))
load OrgDb
load_OrgDb(OrgDb)load_OrgDb(OrgDb)
OrgDb |
OrgDb object or OrgDb name |
OrgDb object
Guangchuang Yu https://yulab-smu.top
data.frame
Convert a list of vectors to a data.frame
ls2df(inputList)ls2df(inputList)
inputList |
List of vectors |
data.frame
convert a matrix to a tidy data frame (from wide to long format as described in the tidyverse concept)
mat2df(x)mat2df(x)
x |
the input matrix |
a data.frame in long format with the 'value' column stores the original values and 'row' and 'col' columns stored in row and column index as in x
Guangchuang Yu
x <- matrix(1:15, nrow = 3) mat2df(x)x <- matrix(1:15, nrow = 3) mat2df(x)
convert a matrix to a list
mat2list(x)mat2list(x)
x |
the input matrix |
a list that contains matrix columns as its elements
x <- matrix(1:15, nrow = 3) mat2list(x)x <- matrix(1:15, nrow = 3) mat2list(x)
Markdown link to a package
mypkg(pkg, url)mypkg(pkg, url)
pkg |
package name |
url |
package url |
md text string
Guangchuang Yu
Other pkg-utils:
CRANpkg(),
Githubpkg(),
get_dependencies(),
get_fun_from_pkg(),
is.installed(),
packageTitle(),
pload()
open selected directory or file
o(file = ".")o(file = ".")
file |
to be open; open working directory by default |
No return value, called for opening specific directory or file
Guangchuang Yu
Other io-utils:
read.cb(),
show_in_excel(),
yread_tsv()
## Not run: ## to open current working directory o() ## End(Not run)## Not run: ## to open current working directory o() ## End(Not run)
Extract package title
packageTitle(pkg, repo = "CRAN")packageTitle(pkg, repo = "CRAN")
pkg |
package name |
repo |
'CRAN' and/or 'BioC' |
reverse dependencies
Guangchuang Yu
Other pkg-utils:
CRANpkg(),
Githubpkg(),
get_dependencies(),
get_fun_from_pkg(),
is.installed(),
mypkg(),
pload()
Parse character ratio to double, e.g., 1/5 → 0.2
parse_ratio(ratio)parse_ratio(ratio)
ratio |
Character vector of ratios |
Numeric vector
Guangchuang Yu
Load a package
pload(package, action = "auto")pload(package, action = "auto")
package |
package name |
action |
Installation function; |
Uses library() to load package. If not installed, attempts installation
via rlang::check_installed() (optionally using BiocManager::install()).
the selected package loaded to the R session
Guangchuang Yu
Other pkg-utils:
CRANpkg(),
Githubpkg(),
get_dependencies(),
get_fun_from_pkg(),
is.installed(),
mypkg(),
packageTitle()
Suppress messages and output from x
quiet(x)quiet(x)
x |
some code |
the result of x
Row-bind a list
rbindlist(x)rbindlist(x)
x |
List with similar elements that can be row-bound |
data.frame
Guangchuang Yu
read clipboard
read.cb(reader = read.table, ...)read.cb(reader = read.table, ...)
reader |
function to read the clipboard |
... |
parameters for the reader |
clipboard content, output type depends on the output of the reader
Guangchuang Yu
Other io-utils:
o(),
show_in_excel(),
yread_tsv()
normalized data by range
scale_range(data)scale_range(data)
data |
the input data. |
normalized data
Guangchuang Yu
using scihub to download publication using doi
scihub_dl(doi, scihub = "sci-hub.tw", download = TRUE)scihub_dl(doi, scihub = "sci-hub.tw", download = TRUE)
doi |
doi |
scihub |
scihub website |
download |
whether download the pdf file |
pdf url
Guangchuang Yu
set_regexpr_style() selects the style explicitly.
auto_set_regexpr_style() chooses based on OS (TRE on Windows; PCRE elsewhere).
set_PCRE() and set_TRE() force the style.
set_PCRE() set_TRE() use_perl() set_regexpr_style(style) auto_set_regexpr_style()set_PCRE() set_TRE() use_perl() set_regexpr_style(style) auto_set_regexpr_style()
style |
one of 'PCRE' or 'TRE' |
These functions do not change the behavior of gsub()/regexpr() directly.
They set a global option that you can read via use_perl() and pass to gsub()/regexpr().
Logical indicating whether to use perl
Guangchuang Yu
https://stackoverflow.com/questions/47240375/regular-expressions-in-base-r-perl-true-vs-the-default-pcre-vs-tre
Open data frame in Excel. It can be used in pipe.
show_in_excel(.data)show_in_excel(.data)
.data |
a data frame to be open |
original .data
Guangchuang Yu
Other io-utils:
o(),
read.cb(),
yread_tsv()
Detect presence/absence of a match
str_detect(string, pattern, negate = FALSE)str_detect(string, pattern, negate = FALSE)
string |
Input string |
pattern |
Pattern to look for |
negate |
If |
Logical vector
Guangchuang Yu
Other str-utils:
str_extract(),
str_starts(),
str_wrap()
Extract a substring using a pattern
str_extract(string, pattern)str_extract(string, pattern)
string |
Input string |
pattern |
Regular expression to extract |
Substring
Guangchuang Yu
Other str-utils:
str_detect(),
str_starts(),
str_wrap()
Detect patterns at the beginning or end of strings
str_starts(string, pattern, negate = FALSE) str_ends(string, pattern, negate = FALSE)str_starts(string, pattern, negate = FALSE) str_ends(string, pattern, negate = FALSE)
string |
Input string |
pattern |
Pattern to match |
negate |
If |
a logical vector
Guangchuang Yu
Other str-utils:
str_detect(),
str_extract(),
str_wrap()
Wrap long strings to multiple lines
str_wrap(string, width = getOption("width"))str_wrap(string, width = getOption("width"))
string |
Input string |
width |
Maximum characters before wrapping |
Updated strings with "\n" inserted
Guangchuang Yu
Other str-utils:
str_detect(),
str_extract(),
str_starts()
get the user dir to save app caches, logs and data (a wrapper function of rappdirs::user_cache_dir())
user_dir(appname = NULL, appauthor = NULL, ...)user_dir(appname = NULL, appauthor = NULL, ...)
appname |
App name |
appauthor |
App author |
... |
additional parameters |
a directory (created if not exists)
Guangchuang Yu
Other os-utils:
exec(),
has_internet()
read file with caching
yread_tsv( file, reader = utils::read.delim, params = list(), cache_dir = tempdir() ) yread(file, reader = readLines, params = list(), cache_dir = NULL)yread_tsv( file, reader = utils::read.delim, params = list(), cache_dir = tempdir() ) yread(file, reader = readLines, params = list(), cache_dir = NULL)
file |
a file or url |
reader |
a function to read the 'file_url' |
params |
a list of parameters that passed to the 'reader' |
cache_dir |
a folder to store cache files. If set to NULL will disable cache. |
This function read a file (local or url) and cache the content.
the output of using the 'reader' to read the 'file_url' with parameters specified by the 'params'
Yonghe Xia and Guangchuang Yu
Other io-utils:
o(),
read.cb(),
show_in_excel()
Other io-utils:
o(),
read.cb(),
show_in_excel()
Provides rlang-based wrappers for messaging: yulab_abort(), yulab_warn(),
and yulab_inform().
yulab_abort(message, class = "yulab_error", ...) yulab_warn(message, class = "yulab_warning", ...) yulab_inform(message, class = "yulab_info", ...)yulab_abort(message, class = "yulab_error", ...) yulab_warn(message, class = "yulab_warning", ...) yulab_inform(message, class = "yulab_info", ...)
message |
Message string |
class |
Custom class for categorization |
... |
Additional context |
No return value
Messages for YuLab packages
yulab_msg(pkgname = NULL, n = 1)yulab_msg(pkgname = NULL, n = 1)
pkgname |
Package name |
n |
Number of citation messages |
Package message
Guangchuang Yu