Title: | Collection of Helper Functions |
---|---|
Description: | Provides a collection of lightweight helper functions (imps) both for interactive use and for inclusion within other packages. These include functions for minimal input assertions, visualising colour palettes, quoting user input, searching rows of a data frame and capturing string tokens. |
Authors: | Tim Taylor [aut, cre, cph] |
Maintainer: | Tim Taylor <[email protected]> |
License: | GPL-3 |
Version: | 1.8.0 |
Built: | 2025-02-05 12:47:35 UTC |
Source: | https://github.com/TimTaylor/ympes |
Assertions for function arguments. Motivated by vctrs::vec_assert()
but
with lower overhead at a cost of less informative error messages. Designed to
make it easy to identify the top level calling function whether used within a
user facing function or internally. They are somewhat experimental in nature
and should be treated accordingly.
assert_integer( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_int( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_integer_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_int_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_double( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_dbl( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_double_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_dbl_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_numeric( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_num( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_dbl_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_numeric_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_num_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_logical( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_lgl( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_logical_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_lgl_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_character( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_chr( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_character_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_chr_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_data_frame( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_list( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_whole( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_integerish( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_integer( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_int( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_integer_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_int_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_double( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_dbl( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_double_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_dbl_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_numeric( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_num( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_numeric_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_num_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_logical( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_lgl( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_logical_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_lgl_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_whole( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_integerish( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_bool( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_boolean( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_character( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_chr( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_string( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_character_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_chr_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_string_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_non_negative_or_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_non_positive_or_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_non_negative( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_non_positive( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_positive( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_negative( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_positive_or_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_negative_or_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_between( x, lower = -Inf, upper = Inf, left_inclusive = TRUE, right_inclusive = TRUE, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL )
assert_integer( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_int( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_integer_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_int_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_double( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_dbl( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_double_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_dbl_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_numeric( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_num( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_dbl_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_numeric_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_num_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_logical( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_lgl( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_logical_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_lgl_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_character( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_chr( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_character_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_chr_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_data_frame( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_list( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_whole( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_integerish( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_integer( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_int( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_integer_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_int_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_double( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_dbl( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_double_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_dbl_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_numeric( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_num( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_numeric_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_num_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_logical( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_lgl( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_logical_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_lgl_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_whole( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_integerish( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_bool( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_boolean( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_character( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_chr( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_string( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_character_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_scalar_chr_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_string_not_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_non_negative_or_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_non_positive_or_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_non_negative( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_non_positive( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_positive( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_negative( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_positive_or_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_negative_or_na( x, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL ) assert_between( x, lower = -Inf, upper = Inf, left_inclusive = TRUE, right_inclusive = TRUE, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = NULL )
x |
Argument to check. |
.arg |
Name of argument being checked (used in error message). |
.call |
Call to use in error message. |
.subclass |
The (optional) subclass of the returned error condition. |
lower |
The lower bound to compare against. |
upper |
The upper bound to compare against. |
left_inclusive |
Should the left (lower) bound be compared inclusively ( |
right_inclusive |
Should the right (upper) bound be compared inclusively ( |
If the assertion succeeds then the input is returned invisibly.
Otherwise an error of class "ympes-error" (with optional subclass if supplied when calling the assertion).
# Use in a user facing function fun <- function(i, d, l, chr, b) { assert_scalar_int(i) TRUE } fun(i=1L) try(fun(i="cat")) # Use in an internal function internal_fun <- function(a) { assert_string( a, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = "example_error" ) TRUE } external_fun <- function(b) { internal_fun(a=b) } external_fun(b="cat") try(external_fun(b = letters)) tryCatch(external_fun(b = letters), error = class)
# Use in a user facing function fun <- function(i, d, l, chr, b) { assert_scalar_int(i) TRUE } fun(i=1L) try(fun(i="cat")) # Use in an internal function internal_fun <- function(a) { assert_string( a, .arg = deparse(substitute(x)), .call = sys.call(-1L), .subclass = "example_error" ) TRUE } external_fun <- function(b) { internal_fun(a=b) } external_fun(b="cat") try(external_fun(b = letters)) tryCatch(external_fun(b = letters), error = class)
cc()
quotes comma separated names whilst trimming outer whitespace. It is
intended for interactive use only.
cc(..., .clip = getOption("imp.clipboard", FALSE))
cc(..., .clip = getOption("imp.clipboard", FALSE))
... |
Either unquoted names (separated by commas) that you wish to quote or a length one character vector you wish to split by whitespace. Empty arguments (e.g. third item in Character vectors not of length one are returned as is. |
.clip |
Should the code to generate the constructed character vector be copied to your system clipboard. Defaults to Note that copying to clipboard requires the availability of package clipr. |
A character vector of the quoted input.
cc(dale, audrey, laura, hawk) cc("dale audrey laura hawk")
cc(dale, audrey, laura, hawk) cc("dale audrey laura hawk")
fstrcapture()
is a more efficient alternative for strcapture()
when
using Perl-compatible regular expressions. It is underpinned by the
regexpr()
function. Whilst fstrcapture()
only returns the first
occurrence of the captures in a string, gstrcapture()
, built upon
gregexpr()
, will return all.
fstrcapture(x, pattern, proto) gstrcapture(x, pattern, proto)
fstrcapture(x, pattern, proto) gstrcapture(x, pattern, proto)
x |
A character vector in which to capture the tokens. |
pattern |
The regular expression with the capture expressions. |
proto |
A |
A tabular data structure of the same type as proto, so typically a data.frame,
containing a column for each capture expression. The column types are
inherited from proto, as are the names unless the captures themselves are
named (in which case these are prioritised). Cases in x that do not match
the pattern have NA in every column. For gstrcapture()
there is an
additional column, string_id
, which links the output to the relevant
element of the input vector.
# from regexpr example ------------------------------------------------- # if named capture then pass names on irrespective of proto notables <- c(" Ben Franklin and Jefferson Davis", "\tMillard Fillmore") pattern <- "(?<first>[[:upper:]][[:lower:]]+) (?<last>[[:upper:]][[:lower:]]+)" proto <- data.frame(a="", b="") fstrcapture(notables, pattern, proto) gstrcapture(notables, pattern, proto) # from strcapture example ---------------------------------------------- # if unnamed capture then proto names used x <- "chr1:1-1000" pattern <- "(.*?):([[:digit:]]+)-([[:digit:]]+)" proto <- data.frame(chr=character(), start=integer(), end=integer()) fstrcapture(x, pattern, proto) # if no proto supplied then all captures treated as character str(fstrcapture(x, pattern)) str(fstrcapture(x, pattern, proto))
# from regexpr example ------------------------------------------------- # if named capture then pass names on irrespective of proto notables <- c(" Ben Franklin and Jefferson Davis", "\tMillard Fillmore") pattern <- "(?<first>[[:upper:]][[:lower:]]+) (?<last>[[:upper:]][[:lower:]]+)" proto <- data.frame(a="", b="") fstrcapture(notables, pattern, proto) gstrcapture(notables, pattern, proto) # from strcapture example ---------------------------------------------- # if unnamed capture then proto names used x <- "chr1:1-1000" pattern <- "(.*?):([[:digit:]]+)-([[:digit:]]+)" proto <- data.frame(chr=character(), start=integer(), end=integer()) fstrcapture(x, pattern, proto) # if no proto supplied then all captures treated as character str(fstrcapture(x, pattern)) str(fstrcapture(x, pattern, proto))
greprows()
searches for pattern matches within a data frames columns and
returns the related rows or row indices.
grepvrows()
is identical to greprows()
except with the default
value = TRUE
.
greplrows()
returns a logical vector (match or not for each row of dat).
greprows( dat, pattern, cols = NULL, value = FALSE, ignore.case = FALSE, perl = FALSE, fixed = FALSE, invert = FALSE ) greplrows( dat, pattern, cols = NULL, ignore.case = FALSE, perl = FALSE, fixed = FALSE, invert = FALSE ) grepvrows( dat, pattern, cols = NULL, value = TRUE, ignore.case = FALSE, perl = FALSE, fixed = FALSE, invert = FALSE )
greprows( dat, pattern, cols = NULL, value = FALSE, ignore.case = FALSE, perl = FALSE, fixed = FALSE, invert = FALSE ) greplrows( dat, pattern, cols = NULL, ignore.case = FALSE, perl = FALSE, fixed = FALSE, invert = FALSE ) grepvrows( dat, pattern, cols = NULL, value = TRUE, ignore.case = FALSE, perl = FALSE, fixed = FALSE, invert = FALSE )
dat |
Data frame |
pattern |
character string containing a regular expression
(or character string for |
cols |
Character vector of columns to search. If |
value |
Should a data frame of rows be returned. If |
ignore.case |
if |
perl |
logical. Should Perl-compatible regexps be used? |
fixed |
logical. If |
invert |
logical. If |
A data frame of the corresponding rows or, if value = FALSE
, the
corresponding row numbers.
dat <- data.frame( first = letters, second = factor(rev(LETTERS)), third = "Q" ) greprows(dat, "A|b") greprows(dat, "A|b", ignore.case = TRUE) greprows(dat, "c", value = FALSE)
dat <- data.frame( first = letters, second = factor(rev(LETTERS)), third = "Q" ) greprows(dat, "A|b") greprows(dat, "A|b", ignore.case = TRUE) greprows(dat, "c", value = FALSE)
new_name()
generates unique names for additional data frame variables
ensuring they are not already present.
new_name(x, n = 1L)
new_name(x, n = 1L)
x |
A data frame. |
n |
Number of unique names to generate. |
A character vector of unique names not already found in x
.
new_name(mtcars) new_name(mtcars, 2)
new_name(mtcars) new_name(mtcars, 2)
plot_palette()
plots a palette from a vector of colour values (name or hex).
plot_palette(values, label = TRUE, square = FALSE)
plot_palette(values, label = TRUE, square = FALSE)
values |
Vector of named or hex colours. |
label |
Do you want to label the plot or not? If |
square |
Display palette as square? |
The input (invisibly).
plot_palette(c("#5FE756", "red", "black")) plot_palette(c("#5FE756", "red", "black"), square = TRUE)
plot_palette(c("#5FE756", "red", "black")) plot_palette(c("#5FE756", "red", "black"), square = TRUE)