suggest_item | R Documentation |
Compares a character scalar to the elements from a character vector and returns the elements that are the closest to the input.
suggest_item(
x,
items,
msg.write = FALSE,
msg.newline = TRUE,
msg.item = "variable"
)
x |
Character scalar, must be provided. This reference will be compared
to the elements of the string vector in the argument |
items |
Character vector, must be provided. Elements to which the value in
argument |
msg.write |
Logical scalar, default is |
msg.newline |
Logical scalar, default is |
msg.item |
Character scalar, default is |
This function is useful when used internally to guide the user to relevant choices.
The choices to which the user is guided are in decreasing quality. First light mispells are checked. Then more important mispells. Finally very important mispells. Completely off potential matches are not reported.
If the argument msg.write
is TRUE
, then a character scalar is returned containing
a message suggesting the matches.
It returns a vector of matches. If no matches were found
Laurent Berge
# function reporting the sum of a variable
sum_var = function(data, var){
# var: a variable name, should be part of data
if(!var %in% names(data)){
suggestion = suggest_item(var, names(data), msg.write = TRUE)
stopi("The variable `{var}` is not in the data set. {suggestion}")
}
sum(data[[var]])
}
# The error message guides us to a suggestion
try(sum_var(iris, "Petal.Le"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.