| Title: | Create and Register Conditions |
|---|---|
| Description: | An interface for creating new condition generators objects. Generators are special functions that can be saved in registries and linked to other functions. Utilities for documenting your generators, and new conditions is provided for package development. |
| Authors: | Jordan Mark Barbone [aut, cph, cre] (ORCID: <https://orcid.org/0000-0001-9788-3628>) |
| Maintainer: | Jordan Mark Barbone <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.1.9002 |
| Built: | 2026-05-31 10:09:08 UTC |
| Source: | https://github.com/jmbarbone/cnd |
This function will create a new object with the name as name
in the environment where it is called. This is intended to be your package
environment, but could potentially be anywhere you want. If an object which
is not a cnd:registry object is found with the same name, an error will be
thrown.
cnd_create_registry( registry = get_package(), overwrite = FALSE, name = ".__CND_REGISTRY__.", env = parent.frame() )cnd_create_registry( registry = get_package(), overwrite = FALSE, name = ".__CND_REGISTRY__.", env = parent.frame() )
registry |
The name of the registry |
overwrite |
When |
name |
The name of the registry variable. Default is intended to prevent potential conflicts with other objects. |
env |
The environment to assign the registry to |
Crate a new cnd:registry to the current environment
a cnd:registry object, invisibly
# In most cases, just having the function in your R/ scripts is good enough, # and you can use `cnd_create_registry()` with its defaults. The following # examples are for demonstration purposes: e <- new.env() cnd_create_registry("EXAMPLE", env = e) cnd_create_registry("EXAMPLE", overwrite = TRUE)# In most cases, just having the function in your R/ scripts is good enough, # and you can use `cnd_create_registry()` with its defaults. The following # examples are for demonstration purposes: e <- new.env() cnd_create_registry("EXAMPLE", env = e) cnd_create_registry("EXAMPLE", overwrite = TRUE)
Documents your conditions()
cnd_document( package = get_package(), registry = package, file = file.path("R", paste0(package, "-cnd-conditions.R")), cleanup = TRUE ) cnd_section(fun)cnd_document( package = get_package(), registry = package, file = file.path("R", paste0(package, "-cnd-conditions.R")), cleanup = TRUE ) cnd_section(fun)
package |
The package to document |
registry |
The name of the registry |
file |
The file to save the documentation. This can be a file path, a
connection object, or |
cleanup |
If |
fun |
The name of a function |
cnd_document() Conditional on the file argument:
when file is a connection, the connection object
when file is a path, the path
when file is NULL, a character vector of the documentation
if no conditions are found, a warning is thrown and NULL is returned
cnd_section() A character vector of the documentation
Conditions are generated through the {cnd} package.
The following conditions are associated with this function:
cnd:cnd_document_conditions/warningDocumentation will fail when no conditions are found. You may be executing cnd_document() too early, before conditions have been registered. You can try to find your conditions with conditions().
cnd:cnd_document_file/errorThe file argument to cnd_document() must be a file path, a connection object, or NULL to return the documentation as a character vector. The default value should be suitable for standard use cases.
cnd:cnd_document_pkg_reg/errorBoth package and registry must be set to document conditions.You can set a registry by adding cnd_create_registry() calls to your package code.
cnd:cnd_generated_cleanup/messageSome files created during the documentation process may become obsolete while updating your conditions.
cnd:cnd_generated_write/conditionThis condition is signaled when cnd_document() needs to write new documentation files.
For more conditions, see: cnd-cnd-conditions
file <- file() cnd_document("cnd", file = file) readLines(file) cnd_section("cnd")file <- file() cnd_document("cnd", file = file) readLines(file) cnd_section("cnd")
[cnd::cnd_exports()] should be used within a package's building
environment.
cnd_exports(env = parent.frame())cnd_exports(env = parent.frame())
env |
The package environment |
Nothing, called for its side-effects
e <- new.env() registry <- cnd_create_registry("EXAMPLE", env = e) local(envir = e, { my_fun <- function() NULL condition( "my_condition", package = "example_package", exports = "my_fun", registry = registry ) cnd_exports() }) # conditions are now added to my_fun(): e$my_fun conditions(e$my_fun)e <- new.env() registry <- cnd_create_registry("EXAMPLE", env = e) local(envir = e, { my_fun <- function() NULL condition( "my_condition", package = "example_package", exports = "my_fun", registry = registry ) cnd_exports() }) # conditions are now added to my_fun(): e$my_fun conditions(e$my_fun)
is functions for cnd
is functions for cnd
is_condition(x) is_cnd_condition(x) is_cnd_generator(x, type = c("error", "warning", "message", "condition")) is_conditioned_function(x)is_condition(x) is_cnd_condition(x) is_cnd_generator(x, type = c("error", "warning", "message", "condition")) is_conditioned_function(x)
x |
An object |
type |
A specific type to check |
TRUE or FALSE for the test
is_condition(simpleCondition("")) is_cnd_condition(simpleCondition("")) con <- condition("is") is_condition(con) is_cnd_condition(con) is_condition(con()) is_cnd_condition(con()) is_cnd_generator(con) is_conditioned_function(cnd)is_condition(simpleCondition("")) is_cnd_condition(simpleCondition("")) con <- condition("is") is_condition(con) is_cnd_condition(con) is_condition(con()) is_cnd_condition(con()) is_cnd_generator(con) is_conditioned_function(cnd)
condition() is used to create a new condition function
that itself returns a new condition.
conditions() retrieves all conditions based on search values. The
parameters serve as filtering arguments.
condition( name, message = NULL, type = c("condition", "message", "warning", "error"), package = get_package(), exports = NULL, help = NULL, registry = package, register = !is.null(registry), classes = NULL, class ) conditions( ..., class = NULL, type = NULL, package = NULL, registry = NULL, fun = NULL ) cond(x) cnd(condition) conditions(x, ...) <- value ## S3 replacement method for class ''function'' conditions(x, append = FALSE, ...) <- value ## S3 replacement method for class ''cnd::condition_progenitor'' conditions(x, ...) <- valuecondition( name, message = NULL, type = c("condition", "message", "warning", "error"), package = get_package(), exports = NULL, help = NULL, registry = package, register = !is.null(registry), classes = NULL, class ) conditions( ..., class = NULL, type = NULL, package = NULL, registry = NULL, fun = NULL ) cond(x) cnd(condition) conditions(x, ...) <- value ## S3 replacement method for class ''function'' conditions(x, append = FALSE, ...) <- value ## S3 replacement method for class ''cnd::condition_progenitor'' conditions(x, ...) <- value
name |
The name of the new class |
message |
The message to be displayed when the condition is called. When entered as a character vector, the message is collapsed into a single string. Use explicit line returns to generate new lines in output messages. When a function is used and a character vector returned, each element is treated as a new line. |
type |
The type of condition: error, warning, or message |
package |
The package to which the condition belongs |
exports |
The exported functions to be displayed when the condition is called |
help |
The help message to be displayed for the condition function |
registry |
The name of the registry to store the condition |
register |
Controls registration checks |
classes |
Additional classes to add to the condition |
class |
Deprecated; use |
... |
Additional arguments passed to methods |
fun |
if a |
x |
An object |
condition |
A condition_generator object |
value |
A |
append |
If |
Conditions
condition() a condition_generator object
conditions() a list of condition_generator objects
cond() A condition_generator object
cnd() is a wrapper for calling base::stop(), base::warning(),
or base::message(); when condition is a type, an error is thrown, and
likewise for the other types. When an error isn't thrown, the condition is
returned, invisibly.
A condition_generator is an
object (a special function) which can be used to create generate a new
condition, based on specifications applied in condition(). These
functions use ... to absorb extra arguments and contain a special .call
parameter. By default, .call captures the parent call from where the
condition_generator was created, but users may pass their own call
to override this. See call. in base::conditionCall()
condition() conditionsConditions are generated through the {cnd} package.
The following conditions are associated with this function:
cnd:condition_as_character_error/errorYou cannot coerce a condition_generator object to a character. This may have occurred when trying to put a condition function through base::stop() or base::warning(). Instead, call the function first, then pass the result to base::stop() or base::warning().
For example:
# Instead of this stop(my_condition) # Do this stop(my_condition())
cnd:condition_message_error/errorConditions messages are displayed when invoked through base::conditionMessage(). You can set a static message by passing through a character vector, or a dynamic message by passing through a function. The function should return a character vector.
When message is not set, a default "there was an error" message is used.
cnd:condition_message_generator_error/errorcondition_generator objects are not conditions. You may have made this mistake:
x <- condition("my_condition")
conditionMessage(x)
Condition generators need to be called first before they can be used as conditions. Try this instead:
x <- condition("my_condition")
conditionMessage(x())
cnd:condition_overwrite_warning/warningDefining a new condition with the same class and package as an existing condition will overwrite the previous definition. It is recommended to either avoid this by fully defining your condition, or creating a new condition instead.
cnd:invalid_condition_error/errorThe class, exports, and help parameters must be a single character string. If you are passing a function, it must be a valid function.
cnd:match_arg/errorMostly base::match.arg() but with a custom condition
cnd:no_package_exports_warning/warningThe exports parameter requires a package
For more conditions, see: cnd-cnd-conditions
cnd() conditionsConditions are generated through the {cnd} package.
The following conditions are associated with this function:
cnd:cnd_class_error/errorcnd() simple calls the appropriate function: base::stop(), base::warning(), or base::message() based on the type parameter from condition().
For more conditions, see: cnd-cnd-conditions
# create a new condition: cond_bad_value <- condition("bad_value", type = "error") # use the condition try(stop(cond_bad_value())) try(cnd(cond_bad_value())) # dynamic messages: cond_class_error <- condition( "class_error", message = function(x) paste("class cannot be", toString(class(x))), type = "error" ) try(stop(cond_class_error(list())))# create a new condition: cond_bad_value <- condition("bad_value", type = "error") # use the condition try(stop(cond_bad_value())) try(cnd(cond_bad_value())) # dynamic messages: cond_class_error <- condition( "class_error", message = function(x) paste("class cannot be", toString(class(x))), type = "error" ) try(stop(cond_class_error(list())))
Formats condition objects
## S3 method for class ''cnd::condition'' format(x, ..., cli = getOption("cnd.cli.override")) ## S3 method for class ''cnd::condition_generator'' format(x, ..., cli = getOption("cnd.cli.override"))## S3 method for class ''cnd::condition'' format(x, ..., cli = getOption("cnd.cli.override")) ## S3 method for class ''cnd::condition_generator'' format(x, ..., cli = getOption("cnd.cli.override"))
x |
A condition object |
... |
Not used |
cli |
If |
A character vector
format(condition("foo"))format(condition("foo"))