Title: | Classed Error and Warning Conditions |
---|---|
Description: | This provides utilities for creating classed error and warning conditions based on where the error originated. |
Authors: | Andrew Redd [aut, cre], R Documentation Task Force [aut] |
Maintainer: | Andrew Redd <[email protected]> |
License: | GPL-2 |
Version: | 0.1.1 |
Built: | 2024-11-12 06:11:51 UTC |
Source: | https://github.com/rdoctaskforce/pkgcond |
The pkgcond package intentionally overrides the assertthat::assert_that()
function. It provides the same utility but enhances the original version
by throwing scoped and typed errors. The type is 'assertion failure' and
the scope can be set or inferred from the calling frame.
assert_that( ..., env = parent.frame(), msg = NULL, scope = find_scope(env), type = "assertion failure" )
assert_that( ..., env = parent.frame(), msg = NULL, scope = find_scope(env), type = "assertion failure" )
... |
unnamed expressions that describe the conditions to be tested.
Rather than combining expressions with |
env |
(advanced use only) the environment in which to evaluate the assertions. |
msg |
a custom error message to be printed if one of the conditions is false. |
scope |
The scope of the error. |
type |
The error type. |
Collapse character Vectors
collapse(x, with = " ") collapse0(x, with = "")
collapse(x, with = " ") collapse0(x, with = "")
x |
a character vector |
with |
character to place between elements of x. |
Use this utility to create nicely formatted lists for error messages and the like.
comma_list(x, sep = ", ", sep2 = " and ", sep.last = ", and ", terminator = "")
comma_list(x, sep = ", ", sep2 = " and ", sep.last = ", and ", terminator = "")
x |
a list that can be converted into a character. |
sep |
the typical separator |
sep2 |
the separator to use in the case of only two elements. |
sep.last |
the separator to use between the last and next to last elements when there are at least 3 element in the list. |
terminator |
concatenated to the end after the list is concluded. |
comma_list(c("you", "I")) comma_list(c("you", "I"), sep2=" & ") comma_list(head(letters), sep.last=', ', term=', ...')
comma_list(c("you", "I")) comma_list(c("you", "I"), sep2=" & ") comma_list(head(letters), sep.last=', ', term=', ...')
Raising Classed conditions helps with catching errors. These allow for typing errors as they arise and adding scopes to better catch errors from specific locations.
condition( msg, cond = .conditions, ..., scope = find_scope(), type = NULL, call = sys.call(1) ) pkg_error(msg, ..., scope = find_scope(), call = sys.call(1)) pkg_warning(msg, ..., scope = find_scope(), call = sys.call(1)) pkg_message(msg, ..., scope = find_scope(), call = sys.call(1))
condition( msg, cond = .conditions, ..., scope = find_scope(), type = NULL, call = sys.call(1) ) pkg_error(msg, ..., scope = find_scope(), call = sys.call(1)) pkg_warning(msg, ..., scope = find_scope(), call = sys.call(1)) pkg_message(msg, ..., scope = find_scope(), call = sys.call(1))
msg |
The message to convey |
cond |
The severity of the condition, or what to do; give a 'message' (default), a 'warning', an 'error' or do 'none' and ignore. |
... |
Attributes to be added to condition object for |
scope |
A character vector of the scope(s) of the signal. Defaults to the package name but could be longer such as package name, a class name, and a method call. This should be used as a where the error occurred. |
type |
Used with |
call |
The call to use to include in the condition. |
The condition()
function alone provides a flexible and dynamic way of
producing conditions in code. The functions pkg_error
, pkg_warning
,
and pkg_message
do the same as condition except restricted to errors, warnings,
and messages respectively.
This shortcut provides simple translation and formatting functionality.
Essentially it is a wrapper for base::gettext()
and base::gettextf()
.
._(msg, ..., domain = NULL)
._(msg, ..., domain = NULL)
msg |
The message to translate. |
... |
Arguments passed on to
|
domain |
see |
loki <- list() class(loki) <- "puny god" ._("I am a %s.", class(loki))
loki <- list() class(loki) <- "puny god" ._("I am a %s.", class(loki))
This find the scope of the call. It includes the package of the call, the class if called from a method, and the name of the function called.
find_scope(frame = NULL, global = FALSE)
find_scope(frame = NULL, global = FALSE)
frame |
The frame to infer scope from. |
global |
Should the global frame be listed in the scope. |
my_function <- function(){ scope <- find_scope() "You are in" %<<% collapse(scope, '::') } my_function() my_sights <- my_function my_sights()
my_function <- function(){ scope <- find_scope() "You are in" %<<% collapse(scope, '::') } my_function() my_sights <- my_function my_sights()
The infix operators listed here are three versions of paste.
%\%
is for preserving line breaks
%<<%
is an infix replacement for paste
%<<<%
is paste with no space and no break."
lhs %<<% rhs lhs %<<<% rhs
lhs %<<% rhs lhs %<<<% rhs
lhs |
left string |
rhs |
right string |
who <- "world" 'hello_' %<<<% who 'Sing with me' %<<% head(letters) %<<% '...'
who <- "world" 'hello_' %<<<% who 'Sing with me' %<<% head(letters) %<<% '...'
The same as %in%
but negated.
x %!in% table
x %!in% table
x |
vector or |
table |
vector or |
'A' %!in% letters #TRUE letters are lower case. 'A' %!in% LETTERS #FALSE LETTERS are upper case.
'A' %!in% letters #TRUE letters are lower case. 'A' %!in% LETTERS #FALSE LETTERS are upper case.
In the course of work it will often be the case that one would like to create a new condition function, such such as for specific errors or warning. These should not be included in the scope when inferred. The natural solution would be to include the scope in every call to condition or have it inferred in each function definition. This however, gets very tedious.
skip_scope(fun)
skip_scope(fun)
fun |
a function to tag |
The skip_scope
function tags a function as one that should be
excluded from consideration when determining scope via
find_scope()
.
The fun
function with the skipscope
attribute set to TRUE.
new_msg <- function(where=find_scope()){ "Hello from" %<<% where } new_postcard <- function(msg){ greeting <- new_msg() paste0(greeting, '\n\n', msg) } cat(new_postcard("Not all is well"), '\n') new_msg <- skip_scope(new_msg) cat(new_postcard("Now all is well"))
new_msg <- function(where=find_scope()){ "Hello from" %<<% where } new_postcard <- function(msg){ greeting <- new_msg() paste0(greeting, '\n\n', msg) } cat(new_postcard("Not all is well"), '\n') new_msg <- skip_scope(new_msg) cat(new_postcard("Now all is well"))
This collection of functions allow the suppression of condition messages, warnings and messages, through filtering the condition message, the condition class or a combination of the two.
suppress_conditions(expr, pattern = NULL, class = NULL, ...) suppress_warnings(expr, pattern = NULL, class = "warning", ...) suppress_messages(expr, pattern = NULL, class = "message", ...)
suppress_conditions(expr, pattern = NULL, class = NULL, ...) suppress_warnings(expr, pattern = NULL, class = "warning", ...) suppress_messages(expr, pattern = NULL, class = "message", ...)
expr |
An expression to evaluate. |
pattern |
A regular expression pattern to match on. |
class |
The class or classes that you would like to filter. When more that one is given the condition may match any of the classes. |
... |
Arguments passed on to
|
suppress_conditions
: The general case of suppressing both messages and warnings.
suppress_warnings
: A convenience wrapper that specifies warning class to suppress.
suppress_messages
: A convenience wrapper that specifies warning class to suppress.
## Not run: testit <- function(){ warning("this function does nothing.") warning("it's pretty useless.") } suppress_warning(testit(), "useless") # Will suppress only the second warning by pattern # If my_pkg used pkgcond for conditions, # This would suppress all messages and warnings originating # in my_pkg functions. suppress_conditions(my_function(), class='my_pkg-condition') ## End(Not run)
## Not run: testit <- function(){ warning("this function does nothing.") warning("it's pretty useless.") } suppress_warning(testit(), "useless") # Will suppress only the second warning by pattern # If my_pkg used pkgcond for conditions, # This would suppress all messages and warnings originating # in my_pkg functions. suppress_conditions(my_function(), class='my_pkg-condition') ## End(Not run)