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.

items

Character vector, must be provided. Elements to which the value in argument x will be compared.

msg.write

Logical scalar, default is FALSE. If TRUE, a message is returned, equal to "Maybe you meant {enum.bq.or ? matches}?" (see stringmagic for information on the interpolation) if there were matches. If no matches were found, the message is "FYI the {msg.item}{$s, are, enum.bq ? items}.".

msg.newline

Logical scalar, default is TRUE. Only used if msg.write = TRUE. Whether to add a new line just before the message.

msg.item

Character scalar, default is "variable". Only used if msg.write = TRUE. What does the items represent?

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"))


[Package dreamerr version 1.4.0 Index]