Title: | Supporting Functions for Packages Maintained by 'YuLab-SMU' |
---|---|
Description: | Miscellaneous functions commonly used by 'YuLab-SMU'. |
Authors: | Guangchuang Yu [aut, cre] |
Maintainer: | Guangchuang Yu <[email protected]> |
License: | Artistic-2.0 |
Version: | 0.1.8.001 |
Built: | 2024-12-03 10:32:30 UTC |
Source: | https://github.com/yulab-smu/yulab.utils |
chunked array
c2(x, y)
c2(x, y)
x |
a vector or chunked_array object |
y |
a vector or chunked_array object |
concate two vector/chunked_array into a chunked_array object
chunked_array object
Guangchuang Yu
Check whether the input packages are installed
check_pkg(pkg, reason = NULL, ...)
check_pkg(pkg, reason = NULL, ...)
pkg |
package names |
reason |
the reason to check the pkg. If NULL, it will set the reason to the parent call. |
... |
additional parameters that passed to |
This function check whether the input packages are installed. If not, it asks the user whether to install the missing packages.
see also check_installed
Guangchuang Yu
all possible combinations of n sets
combinations(n)
combinations(n)
n |
number of sets |
a list of all combinations
print md text of package with link to homepage (CRAN or Bioconductor)
CRANpkg(pkg) Biocpkg(pkg)
CRANpkg(pkg) Biocpkg(pkg)
pkg |
package name |
md text string
Guangchuang Yu
run system command
exec(command)
exec(command)
command |
system command to run |
An exec
instance that stores system command outputs
Guangchuang Yu
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
load function from package
get_fun_from_pkg(pkg, fun)
get_fun_from_pkg(pkg, fun)
pkg |
package |
fun |
function |
function
Guangchuang Yu
get_fun_from_pkg('utils', 'zip')
get_fun_from_pkg('utils', 'zip')
print md text of package with link to github repo
Githubpkg(user, pkg)
Githubpkg(user, pkg)
user |
github user |
pkg |
package name |
md text string
Guangchuang Yu
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
Yulab provides a set of utilities to cache intermediate data, including initialize the cached item, update cached item and rmove the cached item, etc.
initial_cache() get_cache() rm_cache() initial_cache_item(item) get_cache_item(item) rm_cache_item(item) update_cache_item(item, elements) get_cache_element(item, elements)
initial_cache() get_cache() rm_cache() initial_cache_item(item) get_cache_item(item) rm_cache_item(item) update_cache_item(item, elements) get_cache_element(item, elements)
item |
the name of the cached item |
elements |
elements to be cached in the item |
return the cache environment, item or selected elements, depends on the functions.
## 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, args = "--no-build-vignettes")
install_zip(file, args = "--no-build-vignettes")
file |
zip file |
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", args = "--no-build-vignettes")
install_zip_gh(repo, ref = "HEAD", args = "--no-build-vignettes")
repo |
github repo |
ref |
github branch, default is HEAD, which means the default branch of the GitHub repo |
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 the input packages are installed
is.installed(packages)
is.installed(packages)
packages |
package names |
This function check whether the input packages are installed
logical vector
Guangchuang Yu
is.installed(c("dplyr", "ggplot2"))
is.installed(c("dplyr", "ggplot2"))
Convert a list of vector to a data.frame object.
ls2df(inputList)
ls2df(inputList)
inputList |
A list of vector |
a data.frame object.
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)
print md text of link to a pakcage
mypkg(pkg, url)
mypkg(pkg, url)
pkg |
package name |
url |
package url |
md text string
Guangchuang Yu
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
## 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
loading a package
pload(package, action = "auto")
pload(package, action = "auto")
package |
package name |
action |
function used to install package. If 'action = "auto"', it will try to use 'BiocManager::install()' if it is available. |
The function use 'library()' to load the package. If the package is not installed, the function will try to install it before loading it.
the selected package loaded to the R session
Guangchuang Yu
rbind a list
rbindlist(x)
rbindlist(x)
x |
a list that have similar elements that can be rbind to a data.frame |
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
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
The set_regexpr_style()
allows user to specify which style to be used,
while the auto_set_regexpr_style()
automatically set the style depdending on
the operating system (TRE for Windows and PCRE for other OSs (Linux and Mac)).
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' |
set_PCRE()
force to use PCRE style while set_TRE()
force to use TRE.
Note that all these functions are not change the behavior of gsub()
and regexpr()
.
The functions are just set a global option to store the user's choice of whether using perl = TRUE
.
Users can access the option via use_perl()
and pass the return value to gusb()
or regexpr()
to specify the style in use.
logical value of whether use perl
Guangchuang Yu
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
Detect the presentce/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 TRUE, inverts the resulting boolen vector |
logical vector
Guangchuang Yu
Extract a substring using a pattern
str_extract(string, pattern)
str_extract(string, pattern)
string |
input string |
pattern |
a regular expression to describe the pattern to extracted from the 'string' |
substring
Guangchuang Yu
Detect the presence or absence of a pattern at the beginning or end of a string or string vector.
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 with which the string starts or ends |
negate |
if TRUE, return non-matching elements |
a logical vector
Guangchuang Yu
wraping long string to multiple lines
str_wrap(string, width = getOption("width"))
str_wrap(string, width = getOption("width"))
string |
input string |
width |
the maximum number of characters before wrapping to a new line |
update strings with new line character inserted
Guangchuang Yu
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
Messages for R package developed by YuLab
yulab_msg(pkgname = NULL, n = 1)
yulab_msg(pkgname = NULL, n = 1)
pkgname |
package name |
n |
number of citation messages |
package message
Guangchuang Yu