Title: | Check Arguments and Generate Readable Error Messages |
---|---|
Description: | Provides several validator functions for checking if arguments passed by users have valid types, lengths, etc. and for generating informative and well-formatted error messages in a consistent style. Also provides tools for users to create their own validator functions. The error message style used is adopted from <https://style.tidyverse.org/error-messages.html>. |
Authors: | Renfei Mao |
Maintainer: | Renfei Mao <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.6.0 |
Built: | 2025-01-26 02:39:15 UTC |
Source: | https://github.com/flujoo/erify |
Convert an R object to character and add back quotations.
back_quote(x, recursive = TRUE, as_double = TRUE)
back_quote(x, recursive = TRUE, as_double = TRUE)
x |
An R object. |
recursive |
Optional. |
as_double |
Optional. |
A character vector.
back_quote(1:3) back_quote(1:3, recursive = FALSE) back_quote(1:3, as_double = FALSE) back_quote(NULL) back_quote(list(c, 1:3, "a"))
back_quote(1:3) back_quote(1:3, recursive = FALSE) back_quote(1:3, as_double = FALSE) back_quote(NULL) back_quote(list(c, 1:3, "a"))
Check if the arguments of a binary operator have valid classes, and if not, generate an error message.
check_binary_classes( x, y, valid_x, valid_y = NULL, operator = NULL, commutative = NULL, general = NULL, specific = NULL, supplement = NULL, ... )
check_binary_classes( x, y, valid_x, valid_y = NULL, operator = NULL, commutative = NULL, general = NULL, specific = NULL, supplement = NULL, ... )
x , y
|
The argument to check, which can be any object. |
valid_x , valid_y
|
A character vector which contains the valid classes.
|
operator |
Optional. A single character which represents the binary operator. |
commutative |
|
general |
Optional. A single character which is used to give a general statement of the error incurred. By default, this is generated automatically. |
specific |
Optional. A single character which gives a detailed
description of the error. |
supplement |
Optional. A (named) character vector which gives some
additional information about the error. The names are used to create
bullets, see |
... |
Optional. Additional arguments which can be retrieved with
|
returns an invisible NULL
if the argument is valid, or
generates an error message.
"Examples" section in check_type()
for how to customize
error message and how to add and retrieve additional arguments.
vignette("erify")
for a gentle introduction to this package.
## Not run: x <- 1 class(x) <- c("a", "b") y <- 2 class(y) <- c("c", "d") check_binary_classes(x, y, c("d", "e")) check_binary_classes(x, y, c("d", "e"), operator = "+") check_binary_classes(x, y, c("d", "e"), c("a", "f")) check_binary_classes(x, y, c("d", "e"), c("a", "f"), commutative = FALSE) # customize error message with `glue::glue()` syntax check_binary_classes( x, y, c("d", "e"), specific = "Left: {feature_x[1]}, {feature_x[2]}.", supplement = "Right: {feature_y[1]}, {feature_y[2]}." ) ## End(Not run)
## Not run: x <- 1 class(x) <- c("a", "b") y <- 2 class(y) <- c("c", "d") check_binary_classes(x, y, c("d", "e")) check_binary_classes(x, y, c("d", "e"), operator = "+") check_binary_classes(x, y, c("d", "e"), c("a", "f")) check_binary_classes(x, y, c("d", "e"), c("a", "f"), commutative = FALSE) # customize error message with `glue::glue()` syntax check_binary_classes( x, y, c("d", "e"), specific = "Left: {feature_x[1]}, {feature_x[2]}.", supplement = "Right: {feature_y[1]}, {feature_y[2]}." ) ## End(Not run)
Check if an argument is TRUE
or FALSE
, and if not,
generate an error message.
check_bool( x, name = NULL, general = NULL, specific = NULL, supplement = NULL, ... )
check_bool( x, name = NULL, general = NULL, specific = NULL, supplement = NULL, ... )
x |
The argument to check, which can be any object. |
name |
A single character which gives the argument's name.
The name is used in the error message. By default, the name of the
argument passed to argument |
general |
Optional. A single character which is used to give a general statement of the error incurred. By default, this is generated automatically. |
specific |
Optional. A single character which gives a detailed description of the error. By default, this is generated automatically. |
supplement |
Optional. A (named) character vector which gives some
additional information about the error. The names are used to create
bullets, see |
... |
Optional. Additional arguments which can be retrieved with
|
returns an invisible NULL
if the argument is valid, or
generates an error message.
"Examples" section in check_type()
for how to customize
error message and how to add and retrieve additional arguments.
vignette("erify")
for a gentle introduction to this package.
x <- TRUE check_bool(x) ## Not run: # `x` must have type logical x <- 1 check_bool(x) # `x` must have length 1 x <- c(TRUE, FALSE) check_bool(x) # `x` must not be `NA` x <- NA check_bool(x) ## End(Not run)
x <- TRUE check_bool(x) ## Not run: # `x` must have type logical x <- 1 check_bool(x) # `x` must have length 1 x <- c(TRUE, FALSE) check_bool(x) # `x` must not be `NA` x <- NA check_bool(x) ## End(Not run)
Check if an argument has valid class, and if not, generate an error message.
check_class( x, valid, name = NULL, general = NULL, specific = NULL, supplement = NULL, ... )
check_class( x, valid, name = NULL, general = NULL, specific = NULL, supplement = NULL, ... )
x |
The argument to check, which can be any object. |
valid |
A character vector which contains valid classes. |
name |
A single character which gives the argument's name.
The name is used in the error message. By default, the name of the
argument passed to argument |
general |
Optional. A single character which is used to give a general statement of the error incurred. By default, this is generated automatically. |
specific |
Optional. A single character which gives a detailed
description of the error. |
supplement |
Optional. A (named) character vector which gives some
additional information about the error. The names are used to create
bullets, see |
... |
Optional. Additional arguments which can be retrieved with
|
returns an invisible NULL
if the argument is valid, or
generates an error message.
"Examples" section in check_type()
for how to customize
error message and how to add and retrieve additional arguments.
vignette("erify")
for a gentle introduction to this package.
x <- 1 class(x) <- c("a", "b") check_class(x, c("a", "c")) ## Not run: check_class(x, c("c", "d")) # customize error message with `glue::glue()` syntax specific <- "Unbelievable! The first class of `{name}` is {feature[1]}." check_class(x, c("c", "d"), specific = specific) ## End(Not run)
x <- 1 class(x) <- c("a", "b") check_class(x, c("a", "c")) ## Not run: check_class(x, c("c", "d")) # customize error message with `glue::glue()` syntax specific <- "Unbelievable! The first class of `{name}` is {feature[1]}." check_class(x, c("c", "d"), specific = specific) ## End(Not run)
Check if each item of an argument has valid class, and if not, generate an error message.
check_classes( x, valid, name = NULL, general = NULL, specific = NULL, supplement = NULL, ... )
check_classes( x, valid, name = NULL, general = NULL, specific = NULL, supplement = NULL, ... )
x |
The argument to check, which must be a list. |
valid |
A character vector which contains valid classes. |
name |
A single character which gives the argument's name.
The name is used in the error message. By default, the name of the
argument passed to argument |
general |
Optional. A single character which is used to give a general statement of the error incurred. By default, this is generated automatically. |
specific |
Optional. A single character which gives a detailed
description of the error. |
supplement |
Optional. A (named) character vector which gives some
additional information about the error. The names are used to create
bullets, see |
... |
Optional. Additional arguments which can be retrieved with
|
returns an invisible NULL
if the argument is valid, or
generates an error message.
"Examples" section in check_type()
for how to customize
error message and how to add and retrieve additional arguments.
vignette("erify")
for a gentle introduction to this package.
# argument to check arg <- lapply(1:10, function(x) {class(x) <- c("a", "b"); x}) check_classes(arg, "a") ## Not run: check_classes(arg, c("x", "y")) ## End(Not run)
# argument to check arg <- lapply(1:10, function(x) {class(x) <- c("a", "b"); x}) check_classes(arg, "a") ## Not run: check_classes(arg, c("x", "y")) ## End(Not run)
Check if an argument is from some given choices or satisfies some requirement, and if not, generate an error message.
check_content( x, valid, name = NULL, general = NULL, specific = NULL, supplement = NULL, as_double = TRUE, ... )
check_content( x, valid, name = NULL, general = NULL, specific = NULL, supplement = NULL, as_double = TRUE, ... )
x |
The argument to check, which can be any object. |
valid |
can be
|
name |
A single character which gives the argument's name.
The name is used in the error message. By default, the name of the
argument passed to argument |
general |
Optional. A single character which is used to give a general statement of the error incurred. By default, this is generated automatically. |
specific |
Optional. A single character which gives a detailed description of the error. By default, this is generated automatically. |
supplement |
Optional. A (named) character vector which gives some
additional information about the error. The names are used to create
bullets, see |
as_double |
Optional. |
... |
Optional. Additional arguments which can be retrieved with
|
returns an invisible NULL
if the argument is valid, or
generates an error message.
"Examples" section in check_type()
for how to customize
error message and how to add and retrieve additional arguments.
vignette("erify")
for a gentle introduction to this package.
valid <- c(1, 2, 3) x <- 2L check_content(x, valid) ## Not run: # `x` must have the same type with `valid` x <- "a" check_content(x, valid) # `x` must have length 1 x <- c(1, 2) check_content(x, valid) # differentiate between type double and integer x <- 2L check_content(x, valid, as_double = FALSE) # `valid` can be a function check_content(x, is.na, general = "`x` must be `NA`.") # `valid` can be a string of R code check_content(x, "is.na(x)", general = "`x` must be `NA`.") ## End(Not run)
valid <- c(1, 2, 3) x <- 2L check_content(x, valid) ## Not run: # `x` must have the same type with `valid` x <- "a" check_content(x, valid) # `x` must have length 1 x <- c(1, 2) check_content(x, valid) # differentiate between type double and integer x <- 2L check_content(x, valid, as_double = FALSE) # `valid` can be a function check_content(x, is.na, general = "`x` must be `NA`.") # `valid` can be a string of R code check_content(x, "is.na(x)", general = "`x` must be `NA`.") ## End(Not run)
Check if each item of an argument is from some given choices or satisfies some requirement, and if not, generate an error message.
check_contents( x, valid, name = NULL, general = NULL, specific = NULL, supplement = NULL, as_double = TRUE, ... )
check_contents( x, valid, name = NULL, general = NULL, specific = NULL, supplement = NULL, as_double = TRUE, ... )
x |
The argument to check, which can be any object. |
valid |
can be
|
name |
A single character which gives the argument's name.
The name is used in the error message. By default, the name of the
argument passed to argument |
general |
Optional. A single character which is used to give a general statement of the error incurred. By default, this is generated automatically. |
specific |
Optional. A single character which gives a detailed description of the error. By default, this is generated automatically. |
supplement |
Optional. A (named) character vector which gives some
additional information about the error. The names are used to create
bullets, see |
as_double |
Optional. |
... |
Optional. Additional arguments which can be retrieved with
|
returns an invisible NULL
if the argument is valid, or
generates an error message.
"Examples" section in check_type()
for how to customize
error message and how to add and retrieve additional arguments.
vignette("erify")
for a gentle introduction to this package.
## Not run: x <- c(1, 2, 3) check_contents(x, c(4, 5)) general = "Each item of `x` must be `NA`." # `valid` can be a function or R code check_contents(x, is.na, general = general) check_contents(x, "is.na(x_i)", general = general) ## End(Not run)
## Not run: x <- c(1, 2, 3) check_contents(x, c(4, 5)) general = "Each item of `x` must be `NA`." # `valid` can be a function or R code check_contents(x, is.na, general = general) check_contents(x, "is.na(x_i)", general = general) ## End(Not run)
Check if an argument is a number in an interval, and if not, generate an error message.
check_interval( x, valid, name = NULL, general = NULL, specific = NULL, supplement = NULL, ... )
check_interval( x, valid, name = NULL, general = NULL, specific = NULL, supplement = NULL, ... )
x |
The argument to check, which can be any object. |
valid |
A numeric vector of length 2, which represents the valid
closed interval. If |
name |
A single character which gives the argument's name.
The name is used in the error message. By default, the name of the
argument passed to argument |
general |
Optional. A single character which is used to give a general statement of the error incurred. By default, this is generated automatically. |
specific |
Optional. A single character which gives a detailed
description of the error. |
supplement |
Optional. A (named) character vector which gives some
additional information about the error. The names are used to create
bullets, see |
... |
Optional. Additional arguments which can be retrieved with
|
returns an invisible NULL
if the argument is valid, or
generates an error message.
vignette("erify")
for a gentle introduction to this package.
x <- 3.3 check_interval(x, c(1, 5)) ## Not run: check_interval(x, c(1L, 5L)) check_interval(x, c(4, NA)) check_interval(x, c(NA, 2)) ## End(Not run)
x <- 3.3 check_interval(x, c(1, 5)) ## Not run: check_interval(x, c(1L, 5L)) check_interval(x, c(4, NA)) check_interval(x, c(NA, 2)) ## End(Not run)
Check if each item of an argument is a number in an interval, and if not, generate an error message.
check_intervals( x, valid, name = NULL, general = NULL, specific = NULL, supplement = NULL, ... )
check_intervals( x, valid, name = NULL, general = NULL, specific = NULL, supplement = NULL, ... )
x |
The argument to check. |
valid |
A numeric vector of length 2, which represents the valid
closed interval. If |
name |
A single character which gives the argument's name.
The name is used in the error message. By default, the name of the
argument passed to argument |
general |
Optional. A single character which is used to give a general statement of the error incurred. By default, this is generated automatically. |
specific |
Optional. A single character which gives a detailed
description of the error. |
supplement |
Optional. A (named) character vector which gives some
additional information about the error. The names are used to create
bullets, see |
... |
Optional. Additional arguments which can be retrieved with
|
returns an invisible NULL
if the argument is valid, or
generates an error message.
"Examples" section in check_type()
for how to customize
error message and how to add and retrieve additional arguments.
vignette("erify")
for a gentle introduction to this package.
x <- c(1, 3, 5) check_intervals(x, c(0, 6)) ## Not run: check_intervals(x, c(2, 4)) ## End(Not run)
x <- c(1, 3, 5) check_intervals(x, c(0, 6)) ## Not run: check_intervals(x, c(2, 4)) ## End(Not run)
Check if an argument has valid length, and if not, generate an error message.
check_length( x, valid, name = NULL, general = NULL, specific = NULL, supplement = NULL, interval = NULL, ... )
check_length( x, valid, name = NULL, general = NULL, specific = NULL, supplement = NULL, interval = NULL, ... )
x |
The argument to check, which can be any object. |
valid |
A numeric vector which contains non-negative integers and
|
name |
A single character which gives the argument's name.
The name is used in the error message. By default, the name of the
argument passed to argument |
general |
Optional. A single character which is used to give a general statement of the error incurred. By default, this is generated automatically. |
specific |
Optional. A single character which gives a detailed
description of the error. |
supplement |
Optional. A (named) character vector which gives some
additional information about the error. The names are used to create
bullets, see |
interval |
Optional. |
... |
Optional. Additional arguments which can be retrieved with
|
returns an invisible NULL
if the argument is valid, or
generates an error message.
"Examples" section in check_type()
for how to customize
error message and how to add and retrieve additional arguments.
vignette("erify")
for a gentle introduction to this package.
## Not run: x <- c(1, 2) # `valid` as interval check_length(x, c(1, 3), interval = TRUE) check_length(x, c(NA, 2)) # `valid` as single lengths check_length(x, c(1, 3), interval = FALSE) # customize error message with `glue::glue()` syntax specific <- "Oh my god! `{name}`'s length is {feature}." check_length(x, 3, specific = specific) ## End(Not run)
## Not run: x <- c(1, 2) # `valid` as interval check_length(x, c(1, 3), interval = TRUE) check_length(x, c(NA, 2)) # `valid` as single lengths check_length(x, c(1, 3), interval = FALSE) # customize error message with `glue::glue()` syntax specific <- "Oh my god! `{name}`'s length is {feature}." check_length(x, 3, specific = specific) ## End(Not run)
Check if each item of an argument has valid length, and if not, generate an error message.
check_lengths( x, valid, name = NULL, general = NULL, specific = NULL, supplement = NULL, interval = NULL, ... )
check_lengths( x, valid, name = NULL, general = NULL, specific = NULL, supplement = NULL, interval = NULL, ... )
x |
The argument to check, which must be a list. |
valid |
A numeric vector which contains non-negative integers and
|
name |
A single character which gives the argument's name.
The name is used in the error message. By default, the name of the
argument passed to argument |
general |
Optional. A single character which is used to give a general statement of the error incurred. By default, this is generated automatically. |
specific |
Optional. A single character which gives a detailed
description of the error. |
supplement |
Optional. A (named) character vector which gives some
additional information about the error. The names are used to create
bullets, see |
interval |
Optional. |
... |
Optional. Additional arguments which can be retrieved with
|
returns an invisible NULL
if the argument is valid, or
generates an error message.
"Examples" section in check_type()
for how to customize
error message and how to add and retrieve additional arguments.
vignette("erify")
for a gentle introduction to this package.
## Not run: x <- list(1, c(1, 2), c(1, 2, 3)) check_lengths(x, c(1, NA)) specific = "Item {i} has length {feature}." check_lengths(x, c(1, NA), specific = specific) ## End(Not run)
## Not run: x <- list(1, c(1, 2), c(1, 2, 3)) check_lengths(x, c(1, NA)) specific = "Item {i} has length {feature}." check_lengths(x, c(1, NA), specific = specific) ## End(Not run)
Check if an argument is a single natural number, and if not, generate an error message.
Can be used to check indices, for example.
check_n( x, name = NULL, general = NULL, specific = NULL, supplement = NULL, zero = FALSE, ... ) is_n(x, zero = FALSE)
check_n( x, name = NULL, general = NULL, specific = NULL, supplement = NULL, zero = FALSE, ... ) is_n(x, zero = FALSE)
x |
The argument to check, which can be any object. |
name |
A single character which gives the argument's name.
The name is used in the error message. By default, the name of the
argument passed to argument |
general |
Optional. A single character which is used to give a general statement of the error incurred. By default, this is generated automatically. |
specific |
Optional. A single character which gives a detailed description of the error. By default, this is generated automatically. |
supplement |
Optional. A (named) character vector which gives some
additional information about the error. The names are used to create
bullets, see |
zero |
Optional. |
... |
Optional. Additional arguments which can be retrieved with
|
check_n()
returns an invisible NULL
if the argument
is valid, or it generates an error message.
is_n()
returns TRUE
or FALSE
.
"Examples" section in check_type()
for how to customize
error message and how to add and retrieve additional arguments.
vignette("erify")
for a gentle introduction to this package.
x <- 1 check_n(x) x <- 1L check_n(x) sapply(c(1, 2.1, 0, Inf, NA, -9), is_n) ## Not run: # `x` must be a numeric x <- "1" check_n(x) # `x` must have length 1 x <- 1:2 check_n(x) # `x` must not be `NA` x <- NA_integer_ check_n(x) # `x` must be larger than 0 x <- -1 check_n(x) # `x` must be an integer in a mathematical sense x <- 1.1 check_n(x) # make `0` acceptable x <- 0 check_n(x) check_n(x, zero = TRUE) ## End(Not run)
x <- 1 check_n(x) x <- 1L check_n(x) sapply(c(1, 2.1, 0, Inf, NA, -9), is_n) ## Not run: # `x` must be a numeric x <- "1" check_n(x) # `x` must have length 1 x <- 1:2 check_n(x) # `x` must not be `NA` x <- NA_integer_ check_n(x) # `x` must be larger than 0 x <- -1 check_n(x) # `x` must be an integer in a mathematical sense x <- 1.1 check_n(x) # make `0` acceptable x <- 0 check_n(x) check_n(x, zero = TRUE) ## End(Not run)
Check if an argument is a single positive number, and if not, generate an error message.
check_positive( x, name = NULL, general = NULL, specific = NULL, supplement = NULL, zero = FALSE, ... )
check_positive( x, name = NULL, general = NULL, specific = NULL, supplement = NULL, zero = FALSE, ... )
x |
The argument to check, which can be any object. |
name |
A single character which gives the argument's name.
The name is used in the error message. By default, the name of the
argument passed to argument |
general |
Optional. A single character which is used to give a general statement of the error incurred. By default, this is generated automatically. |
specific |
Optional. A single character which gives a detailed description of the error. By default, this is generated automatically. |
supplement |
Optional. A (named) character vector which gives some
additional information about the error. The names are used to create
bullets, see |
zero |
Optional. |
... |
Optional. Additional arguments which can be retrieved with
|
returns an invisible NULL
if the argument is valid, or
generates an error message.
"Examples" section in check_type()
for how to customize
error message and how to add and retrieve additional arguments.
vignette("erify")
for a gentle introduction to this package.
x <- 1.1 check_positive(x) x <- 1L check_positive(x) ## Not run: # `x` must be a numeric x <- "1" check_positive(x) # `x` must have length 1 x <- 1:2 check_positive(x) # `x` must not be `NA` x <- NA_integer_ check_positive(x) # `x` must be larger than 0 x <- -1 check_positive(x) # make `0` acceptable x <- 0 check_positive(x) check_positive(x, zero = TRUE) ## End(Not run)
x <- 1.1 check_positive(x) x <- 1L check_positive(x) ## Not run: # `x` must be a numeric x <- "1" check_positive(x) # `x` must have length 1 x <- 1:2 check_positive(x) # `x` must not be `NA` x <- NA_integer_ check_positive(x) # `x` must be larger than 0 x <- -1 check_positive(x) # make `0` acceptable x <- 0 check_positive(x) check_positive(x, zero = TRUE) ## End(Not run)
Check if an argument is a single character. and if not, generate an error message.
Can be used to check argument names, for example.
check_string( x, name = NULL, general = NULL, specific = NULL, supplement = NULL, ... ) is_string(x)
check_string( x, name = NULL, general = NULL, specific = NULL, supplement = NULL, ... ) is_string(x)
x |
The argument to check, which can be any object. |
name |
A single character which gives the argument's name.
The name is used in the error message. By default, the name of the
argument passed to argument |
general |
Optional. A single character which is used to give a general statement of the error incurred. By default, this is generated automatically. |
specific |
Optional. A single character which gives a detailed description of the error. By default, this is generated automatically. |
supplement |
Optional. A (named) character vector which gives some
additional information about the error. The names are used to create
bullets, see |
... |
Optional. Additional arguments which can be retrieved with
|
returns an invisible NULL
if the argument is valid, or
generates an error message.
"Examples" section in check_type()
for how to customize
error message and how to add and retrieve additional arguments.
vignette("erify")
for a gentle introduction to this package.
x <- "a" check_string(x) ## Not run: # `x` must have type character x <- c check_string(x) # `x` must have length 1 x <- c("a", "b") check_string(x) # `NA_character_` is not acceptable x <- NA_character_ check_string(x) ## End(Not run)
x <- "a" check_string(x) ## Not run: # `x` must have type character x <- c check_string(x) # `x` must have length 1 x <- c("a", "b") check_string(x) # `NA_character_` is not acceptable x <- NA_character_ check_string(x) ## End(Not run)
Check if an argument has valid type, and if not, generate an error message.
check_type( x, valid, name = NULL, general = NULL, specific = NULL, supplement = NULL, ... )
check_type( x, valid, name = NULL, general = NULL, specific = NULL, supplement = NULL, ... )
x |
The argument to check, which can be any object. |
valid |
A character vector which contains the valid types. |
name |
A single character which gives the argument's name.
The name is used in the error message. By default, the name of the
argument passed to argument |
general |
Optional. A single character which is used to give a general statement of the error incurred. By default, this is generated automatically. |
specific |
Optional. A single character which gives a detailed
description of the error. |
supplement |
Optional. A (named) character vector which gives some
additional information about the error. The names are used to create
bullets, see |
... |
Optional. Additional arguments which can be retrieved with
|
returns an invisible NULL
if the argument is valid, or
generates an error message.
vignette("erify")
for a gentle introduction to this package.
# argument to check arg <- 10 # returns silently if the argument has valid type check_type(arg, "double") ## Not run: check_type(arg, "character") # specify argument's name check_type(arg, "character", name = "x") # specify argument `specific` with `glue::glue()` syntax specific <- "`{name}`'s type is {feature}, which is wrong." check_type(arg, "character", specific = specific) # specify argument `supplement` supplement <- c("You're wrong.", i = "Check your code.") check_type(arg, "character", supplement = supplement) # turn off `specific` check_type(arg, "character", specific = character()) ## End(Not run) # add and retrieve additional argument tryCatch( {check_type(arg, "character", your_arg = "your data")}, error = function(e) e$your_arg )
# argument to check arg <- 10 # returns silently if the argument has valid type check_type(arg, "double") ## Not run: check_type(arg, "character") # specify argument's name check_type(arg, "character", name = "x") # specify argument `specific` with `glue::glue()` syntax specific <- "`{name}`'s type is {feature}, which is wrong." check_type(arg, "character", specific = specific) # specify argument `supplement` supplement <- c("You're wrong.", i = "Check your code.") check_type(arg, "character", supplement = supplement) # turn off `specific` check_type(arg, "character", specific = character()) ## End(Not run) # add and retrieve additional argument tryCatch( {check_type(arg, "character", your_arg = "your data")}, error = function(e) e$your_arg )
Check if each item of an argument has valid type, and if not, generate an error message.
check_types( x, valid, name = NULL, general = NULL, specific = NULL, supplement = NULL, ... )
check_types( x, valid, name = NULL, general = NULL, specific = NULL, supplement = NULL, ... )
x |
The argument to check, which must be a list. |
valid |
A character vector which contains the valid types. |
name |
A single character which gives the argument's name.
The name is used in the error message. By default, the name of the
argument passed to argument |
general |
Optional. A single character which is used to give a general statement of the error incurred. By default, this is generated automatically. |
specific |
Optional. A single character which gives a detailed
description of the error. |
supplement |
Optional. A (named) character vector which gives some
additional information about the error. The names are used to create
bullets, see |
... |
Optional. Additional arguments which can be retrieved with
|
returns an invisible NULL
if the argument is valid, or
generates an error message.
"Examples" section in check_type()
for how to customize
error message and how to add and retrieve additional arguments.
vignette("erify")
for a gentle introduction to this package.
## Not run: # argument to check arg <- as.list(1:10) check_types(arg, "character") # customize error message with `glue::glue()` syntax specific <- "`{name}[[{i}]]` is an {feature}, oh my god!" check_types(arg, "character", specific = specific) ## End(Not run)
## Not run: # argument to check arg <- as.list(1:10) check_types(arg, "character") # customize error message with `glue::glue()` syntax specific <- "`{name}[[{i}]]` is an {feature}, oh my god!" check_types(arg, "character", specific = specific) ## End(Not run)
Connect given words with a conjunction, e.g. "and" and "or".
join(words, conjunction = "or")
join(words, conjunction = "or")
words |
A vector of list whose items can be converted to characters. |
conjunction |
A single character which represents a conjunction word.
The default value is |
If has length 1 or less, words
is returned. Or items of words
are concatenated and returned.
words <- c("apple", "orange", "Pink Floyd") join(words, "and")
words <- c("apple", "orange", "Pink Floyd") join(words, "and")
Generate and signal a condition.
throw(general, specifics = NULL, env = NULL, as = "error", class = NULL, ...)
throw(general, specifics = NULL, env = NULL, as = "error", class = NULL, ...)
general |
A single character which gives a general statement of the condition. |
specifics |
Optional. A character vector which gives a list of details
of the condition. If is |
env |
Optional. An environment or named list which is used to evaluate
the R code in the above arguments. See |
as |
Optional. |
class |
Optional. A character vector which assigns classes to the condition. |
... |
Optional. Additional arguments which are stored in the
condition and can be retrieved with |
If specifics
is character(0)
, returns an invisible NULL
.
Or signals an error, a warning, or a message.
general <- "You are wrong." # returns silently throw(general, character(0)) ## Not run: throw(general) specifics <- c("Detail 1.", i = "Detail 2.") throw(general, specifics) # embed R code with glue syntax throw("`x` is {x}.", env = list(x = 1)) ## End(Not run) # add and retrieve additional argument tryCatch( { throw(general, arg = "I'm an additional argument.") }, error = function(e) e$arg )
general <- "You are wrong." # returns silently throw(general, character(0)) ## Not run: throw(general) specifics <- c("Detail 1.", i = "Detail 2.") throw(general, specifics) # embed R code with glue syntax throw("`x` is {x}.", env = list(x = 1)) ## End(Not run) # add and retrieve additional argument tryCatch( { throw(general, arg = "I'm an additional argument.") }, error = function(e) e$arg )