plural {dreamerr} | R Documentation |
Adds an s and/or a singular/plural verb depending on the argument's length
Description
Utilities to write user-level messages. These functions add an ‘s’ or a verb at the appropriate form depending on whether the argument is equal to unity (plural
) or of length one (plural_len
).
Usage
plural(x, type, s, verb = FALSE, past = FALSE)
plural_len(x, type, s, verb = FALSE, past = FALSE)
Arguments
x |
An integer of length one ( |
type |
Character string, default is missing. If |
s |
Logical, used only if the argument type is missing. Whether to add an "s" if the form of |
verb |
Character string or |
past |
Logical, used only if the argument type is missing. Whether the verb should be in past tense. Default is |
Value
Returns a character string of length one.
Functions
-
plural_len()
: Adds an s and conjugate a verb depending on the length ofx
Author(s)
Laurent Berge
Examples
# Let's create an error message when NAs are present
my_crossprod = function(mat){
if(anyNA(mat)){
row_na = which(rowSums(is.na(mat)) > 0)
n_na = length(row_na)
stop("In argument 'mat': ", n_letter(n_na), " row", plural(n_na, "s.contain"),
" NA values (", ifelse(n_na<=3, "", "e.g. "), "row",
enumerate_items(head(row_na, 3), "s"),
"). Please remove ", ifunit(n_na, "it", "them"), " first.")
}
crossprod(mat)
}
mat = matrix(rnorm(30), 10, 3)
mat4 = mat1 = mat
mat4[c(1, 7, 13, 28)] = NA
mat1[7] = NA
# Error raised because of NA: informative (and nice) messages
try(my_crossprod(mat4))
try(my_crossprod(mat1))