suggest_item {dreamerr} | R Documentation |
Suggest the the closest elements from a string vector
Description
Compares a character scalar to the elements from a character vector and returns the elements that are the closest to the input.
Usage
suggest_item(
x,
items,
msg.write = FALSE,
msg.newline = TRUE,
msg.item = "variable"
)
Arguments
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 |
Details
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.
Value
It returns a vector of matches. If no matches were found
Author(s)
Laurent Berge
Examples
# 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"))