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.
Usage
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 class '`function`'
conditions(x, append = FALSE, ...) <- value
# S3 method for class '`cnd::condition_progenitor`'
conditions(x, ...) <- value
Arguments
- class
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
- ...
Additional arguments passed to methods
- fun
if a function is passed, then retrieves the
"conditions"
attribute- x
An object
- condition
A condition_generator object
- value
A
condition
- append
If
TRUE
, adds to the conditions attribute
Value
condition()
a condition_generator object
conditions()
alist
of condition_generator objects
cond()
A condition_generator object
condition_generator
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()
condition()
conditions
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 tostop()
orwarning()
.For example:
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
, andhelp
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 acharacter
vector, or a dynamic message by passing through afunction
. The function should return acharacter
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 conditioncnd:no_package_exports/warning
The
exports
parameter requires apackage
For more conditions, see: cnd-cnd-conditions
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()
, ormessage()
based on thetype
parameter fromcondition()
.
For more conditions, see: cnd-cnd-conditions
Examples
# create a new condition:
cond_bad_value <- condition("bad_value", type = "error")
# use the condition
try(stop(cond_bad_value()))
#> Error in eval() : <bad_value>
#> there was an error
try(cnd(cond_bad_value()))
#> Error in eval() : <bad_value>
#> there was an error
# 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())))
#> Error in eval() : <class_error>
#> class cannot be list