Title: | What the Package Does (One Line, Title Case) |
---|---|
Description: | What the package does (one paragraph). |
Authors: | First Last [aut, cre] (YOUR-ORCID-ID) |
Maintainer: | First Last <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.0.0.9000 |
Built: | 2024-11-21 02:48:49 UTC |
Source: | https://github.com/selkamand/utilitybeltassertions |
Check if test_values are one of those described in 'set' vector
assert_all_values_are_in_set( test_values, acceptable_values, name = rlang::caller_arg(test_values) )
assert_all_values_are_in_set( test_values, acceptable_values, name = rlang::caller_arg(test_values) )
test_values |
values to check are equal to one of the values in set (character/numeric) |
acceptable_values |
valid options for elements in test_values (character/numeric) |
name |
name used to describe test values. Used in error message reporting (i.e. 'name' must be one of <acceptable_values>) |
set = c("A", "B", "C") letters = c("A", "B", "B") assert_all_values_are_in_set(letters, set)
set = c("A", "B", "C") letters = c("A", "B", "B") assert_all_values_are_in_set(letters, set)
Take a filename / vector of filenames and assert that they all end with one of the user-supplied 'valid extensions'
assert_filenames_have_valid_extensions( filenames, valid_extensions, ignore_case = TRUE )
assert_filenames_have_valid_extensions( filenames, valid_extensions, ignore_case = TRUE )
filenames |
filenames to assert has a valid extension (character) |
valid_extensions |
all possible valid extensions (character) |
ignore_case |
does the case (uppercase/lowercase) of the extensions matter? (bool) |
# Ensure filename has a "fasta" or 'fa' extension assert_filenames_have_valid_extensions( filename="sequence.fasta", valid_extensions = c("fasta", "fa") )
# Ensure filename has a "fasta" or 'fa' extension assert_filenames_have_valid_extensions( filename="sequence.fasta", valid_extensions = c("fasta", "fa") )
Do all files exist
assert_files_exist(filepaths, supplementary_error_message = "")
assert_files_exist(filepaths, supplementary_error_message = "")
filepaths |
filepaths (character) |
supplementary_error_message |
supplementary error message - e.g. a line describing what functions to run to produce required files (string) |
## Not run: assert_files_exist( c("/path/to/file1", "/path/to/file2"), supplementary_error_message = "Please run X to produce files you need" ) ## End(Not run)
## Not run: assert_files_exist( c("/path/to/file1", "/path/to/file2"), supplementary_error_message = "Please run X to produce files you need" ) ## End(Not run)
Checks if object is a whole number (e.g. 1, 5, 5.0, 6.000). Vectors are flagged as NOT whole numbers (intentional behaviour).
assert_is_whole_number(object, msg = "")
assert_is_whole_number(object, msg = "")
object |
Some value you want to assert is a whole number (single scalar value) |
msg |
Some message to print on failure (appended to the hard-coded message). Will automatically get wrapped in utilitybeltfmt::fmterror (string) |
invisible(TRUE) if the object passes the assertion. Throws an error if it does not.
Other customassertions:
assert_non_empty_string()
,
assert_that_invisible()
assert_is_whole_number(5)
assert_is_whole_number(5)
assert_names_include
assert_names_include(object, expected_names, object_name_in_error_message = NA)
assert_names_include(object, expected_names, object_name_in_error_message = NA)
object |
an object (usually vector or dataframe) that you want to assert has certain names |
expected_names |
names you expect the object to have (order doesn't matter) (character vector) |
object_name_in_error_message |
how to refer to the object in the error message |
assert_names_include(mtcars, expected_names = c("mpg", "cyl"))
assert_names_include(mtcars, expected_names = c("mpg", "cyl"))
Check object is a non-empty string
assert_non_empty_string(object, msg = "")
assert_non_empty_string(object, msg = "")
object |
Some value you want to assert is a non-empty string |
msg |
Some message to print on failure (appended to the hard-coded message). Will automatically get wrapped in utilitybeltfmt::fmterror (string) |
invisible(TRUE) if the object is a non-empty string. Throws an error if it is not.
Other customassertions:
assert_is_whole_number()
,
assert_that_invisible()
possiblestring = "Billy" assert_non_empty_string(possiblestring)
possiblestring = "Billy" assert_non_empty_string(possiblestring)
Check if program is available in path Should work on all operating systems
assert_program_exists_in_path(program_names)
assert_program_exists_in_path(program_names)
program_names |
name/s of program to search for in path (character) |
## Not run: assert_program_exists_in_path(c("grep", "wget")) ## End(Not run)
## Not run: assert_program_exists_in_path(c("grep", "wget")) ## End(Not run)
Wraps around asserthat::assertthat::assert_that() but makes return value invisible (can be assigned but will not print when not assigned)
assert_that_invisible(..., env = parent.frame(), msg = NULL)
assert_that_invisible(..., env = parent.frame(), msg = NULL)
... |
see ?assertthat::assert_that |
env |
see ?assertthat::assert_that |
msg |
see ?assertthat::assert_that |
invisible (TRUE) if expression is TRUE. Will error if is FALSE
Other customassertions:
assert_is_whole_number()
,
assert_non_empty_string()
Check if object has a particular class
class_is(object, tested_class)
class_is(object, tested_class)
object |
object whose class you want to check (object) |
tested_class |
class (string) |
TRUE if object class matches tested_class. FALSE if not.
A collection of functions that take text and return that same text flanked by characters that will lead to its coloration/formatting when printed to terminals using message/cat. Different presets are available: utilitybeltfmt::fmterror, utilitybeltfmt::fmtwarning, utilitybeltfmt::fmtsuccess, utilitybeltfmt::fmtbold.
If greater control is required, use the crayon package
fmtbold(...)
fmtbold(...)
... |
(string/s) Text to colorise. Comma separated strings will be concatenated (no spaces) before colorisation. |
(string) Input text flanked by relevant Ansi escape codes
A collection of functions that take text and return that same text flanked by characters that will lead to its coloration/formatting when printed to terminals using message/cat. Different presets are available: utilitybeltfmt::fmterror, utilitybeltfmt::fmtwarning, utilitybeltfmt::fmtsuccess, utilitybeltfmt::fmtbold.
If greater control is required, use the crayon package
fmterror(...)
fmterror(...)
... |
(string/s) Text to colorise. Comma separated strings will be concatenated (no spaces) before colorisation. |
(string) Input text flanked by relevant Ansi escape codes
message(utilitybeltfmt::fmterror("This is a warning"))
message(utilitybeltfmt::fmterror("This is a warning"))
A collection of functions that take text and return that same text flanked by characters that will lead to its coloration/formatting when printed to terminals using message/cat. Different presets are available: utilitybeltfmt::fmterror, utilitybeltfmt::fmtwarning, utilitybeltfmt::fmtsuccess, utilitybeltfmt::fmtbold.
If greater control is required, use the crayon package
fmtsuccess(...)
fmtsuccess(...)
... |
(string/s) Text to colorise. Comma separated strings will be concatenated (no spaces) before colorisation. |
(string) Input text flanked by relevant Ansi escape codes
A collection of functions that take text and return that same text flanked by characters that will lead to its coloration/formatting when printed to terminals using message/cat. Different presets are available: utilitybeltfmt::fmterror, utilitybeltfmt::fmtwarning, utilitybeltfmt::fmtsuccess, utilitybeltfmt::fmtbold.
If greater control is required, use the crayon package
fmtwarning(...)
fmtwarning(...)
... |
(string/s) Text to colorise. Comma separated strings will be concatenated (no spaces) before colorisation. |
(string) Input text flanked by relevant Ansi escape codes
Check how many arguments a function takes.
fun_count_arguments(FUN)
fun_count_arguments(FUN)
FUN |
a function whose arguments we're going to count (function) |
Can be called from inside or outside the function, however if calling from inside a function, the function must be named.
the number of arguments the function takes (numeric)
# Calling from outside a named function fun = function(a, b, c) { return(a+b-c) } fun_count_arguments(fun) ##Returns 3 # Calling from outside an anonymous fun_count_arguments(function(a, b, c) { return(a+b+c) }) ##Returns 3 # Calling from inside a named function my_function <- function(a, b, c, d, e, f, g) { return(fun_count_arguments(my_function)) } my_function() # Equals 7
# Calling from outside a named function fun = function(a, b, c) { return(a+b-c) } fun_count_arguments(fun) ##Returns 3 # Calling from outside an anonymous fun_count_arguments(function(a, b, c) { return(a+b+c) }) ##Returns 3 # Calling from inside a named function my_function <- function(a, b, c, d, e, f, g) { return(fun_count_arguments(my_function)) } my_function() # Equals 7
Looks through stack trace to identify the function that called the current function.
get_calling_function(n_function_calls_ago = 1, verbose = TRUE)
get_calling_function(n_function_calls_ago = 1, verbose = TRUE)
n_function_calls_ago |
position in the stack trace we're interested in. By default will return the name of the function containing the line: get_calling_function(). Setting to 1 will get the call 1 up in the stack trace.(integer) |
verbose |
print informative messages |
the function call as a string. If function is called from the base environment or n_function_calls_ago is set higher than the number of calls in the stack trace, will return 'base' (string)
wrapper <- function(n_function_calls_ago=1, verbose=TRUE){ get_calling_function(n_function_calls_ago, verbose) } wrapper() # Returns"wrapper()" wrapper_wrapper <- function(n_function_calls_ago=1, verbose=TRUE){ wrapper(n_function_calls_ago, verbose=verbose) } wrapper_wrapper() # Returns "wrapper(n_function_calls_ago, verbose = verbose)" wrapper_wrapper(2) # Returns "wrapper_wrapper(2)" wrapper_wrapper(100, verbose=FALSE) # Returns "base" wrapper_wrapper(100, verbose=TRUE) # Returns "base" + message
wrapper <- function(n_function_calls_ago=1, verbose=TRUE){ get_calling_function(n_function_calls_ago, verbose) } wrapper() # Returns"wrapper()" wrapper_wrapper <- function(n_function_calls_ago=1, verbose=TRUE){ wrapper(n_function_calls_ago, verbose=verbose) } wrapper_wrapper() # Returns "wrapper(n_function_calls_ago, verbose = verbose)" wrapper_wrapper(2) # Returns "wrapper_wrapper(2)" wrapper_wrapper(100, verbose=FALSE) # Returns "base" wrapper_wrapper(100, verbose=TRUE) # Returns "base" + message