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)

Arguments

x

An integer of length one (plural) or a vector plural_len.

type

Character string, default is missing. If type = "s.is.past" it means that an "s" will be added if x is greater than 1 (or of length greater than one for plural_len); it will be followed by the verb "to be" in past tense in singular or plural form depending on x. This argument must be made of keywords separated by points without space, the keywords are "s", "past" and a verb (i.e. any thing different than "s" and "past"). Missing keywords mean their value is equal to FALSE.

s

Logical, used only if the argument type is missing. Whether to add an "s" if the form of x is plural. Default is missing: equals to TRUE if no other argument is provided, FALSE otherwise.

verb

Character string or FALSE, used only if the argument type is missing. The verb to be inserted in singular or plural depending on the value of x. default is FALSE.

past

Logical, used only if the argument type is missing. Whether the verb should be in past tense. Default is FALSE.

Value

Returns a character string of length one.

Functions

  • plural_len(): Adds an s and conjugate a verb depending on the length of x

Author

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))
#> Error in my_crossprod(mat4) : 
#>   In argument 'mat': four rows contain NA values (e.g. rows 1, 3 and 7). Please remove them first.
try(my_crossprod(mat1))
#> Error in my_crossprod(mat1) : 
#>   In argument 'mat': one row contains NA values (row 7). Please remove it first.