plural | R Documentation |
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
).
plural(x, type, s, verb = FALSE, past = FALSE)
plural_len(x, type, s, verb = FALSE, past = FALSE)
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 |
Returns a character string of length one.
plural_len()
: Adds an s and conjugate a verb depending on the length of x
Laurent Berge
# 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))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.