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(
class,
message = NULL,
type = c("condition", "message", "warning", "error"),
package = get_package(),
exports = NULL,
help = NULL,
registry = package,
register = !is.null(registry)
)conditions(
...,
class = NULL,
type = NULL,
package = NULL,
registry = NULL,
fun = NULL
)
cond(x)
cnd(condition)
conditions(x, ...) <- value
# S3 method for `function`
conditions(x, append = FALSE, ...) <- value
# S3 method for `cnd::condition_progenitor`
conditions(x, ...) <- value
condition()
a condition_generator object
conditions()
a list
of condition_generator objects
cond()
A condition_generator object
The name of the new class
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.
The type of condition: error, warning, or message
The package to which the condition belongs
The exported functions to be displayed when the condition is called
The help message to be displayed for the condition function
The name of the registry to store the condition
Controls registration checks
Additional arguments passed to methods
if a function is passed, then retrieves the "conditions"
attribute
An object
A condition_generator object
A condition
If TRUE
, adds to the conditions attribute
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 conditionCall()
Conditions are generated through the {cnd}
package.
The following conditions are associated with this function:
cnd:as_character_cnd_error/error
You cannot coerce a condition_generator object to a character. This may have occurred when trying to put a condition function through stop()
or warning. Instead, call the function first, then pass the result to stop()
or warning()
.
For example:
# Instead of this
stop(my_condition)
# Do this stop(my_condition())
cnd:condition_message_generator/error
condition_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
cnd:invalid_condition/error
The class
, exports
, and help
parameters must be a single character string. If you are passing a function, it must be a valid function.
cnd:invalid_condition_message/error
Conditions messages are displayed when invoked through 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:match_arg/error
Mostly match.arg()
but with a custom condition
cnd:no_package_exports/warning
The exports
parameter requires a package
For more conditions, see: cnd-cnd-conditions
Conditions are generated through the {cnd}
package.
The following conditions are associated with this function:
cnd:cond_cnd_class/error
cnd()
simple calls the appropriate function: stop()
, warning()
, or message()
based on the type
parameter from condition()
.
For more conditions, see: cnd-cnd-conditions
Conditions
cnd-package
# 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())))
Run the code above in your browser using DataLab