Most search engines have a "did you mean?" feature, where suggestions are given in the presence of likely typos. And while search engines use sophisticated NLP methods on their vast amounts of user-generated data to create accurate suggestions, it turns out that you can get by with some ancient spellchecker techniques.
The development version is maintained on GitHub, and can easily be installed by any of the packages that offer installations from GitHub:
### Pick your preference devtools::install_github("wrathematics/Rdym") ghit::install_github("wrathematics/Rdym") remotes::install_github("wrathematics/Rdym")
Usage of the package is completely passive, beyond loading it with the usual
library(Rdym) call. Say for example you run:
shapro.test(x=rnorm(20)) # Error: object 'shapro.test' not found
Note the missing "i" in what should be
shapiro.test(). With Rdym loaded, you can get a "did you mean?" suggestion along with the error:
library(Rdym) shapro.test(x=rnorm(20)) # Error: could not find function "shapro.test" # # Did you mean: shapiro.test() ? # shapiro.test(x=rnorm(20))
If the spellchecker guessed correctly, then you should be able to just copy/paste the suggestion after the "Did you mean" line into R.
Suggestions are given as errors are discovered by the R interpreter. For example:
library(Rdym) shapro.test(rmorm(20)) # Error: could not find function "shapro.test" # # Did you mean: shapiro.test ? # shapiro.test(rmorm(20)) shapiro.test(rmorm(20)) # Error in stopifnot(is.numeric(x)) : could not find function "rmorm" # # Did you mean: rnorm ? # shapiro.test(rnorm(20)) shapiro.test(rnorm(20)) # Shapiro-Wilk normality test # # data: rnorm(20) # W = 0.9366, p-value = 0.207
The package also has an explicit interface:
Rdym::RdymEnable() nonsense # Error: object 'nonsense' not found # # Did you mean: license ? # license Rdym::RdymDisable() nonsense # Error: object 'nonsense' not found
How it works
When R detects that a function or object listed in the user's input is not found, the package finds the minimum Levenshtein distance between the "unfound" token and all symbols in the user's global environment plus all loaded namespaces. The word with minimum Levenshtein distance (in the event of ties, the first such detected) is then suggested as an alternative to the missing symbol.
Fairly efficient C code is used to compute the Levenshtein distances. The "error interception" is just using R's
options() to set a function to run post error (as seen here). The package won't work with batch mode R, so you have to use it in an interactive R session.
Also keep in mind this is basically just a toy. You shouldn't think of this as being in the same class of capabilities as a search engine's suggester.
Natural Language Support
We try to support all localizations available to R. For example:
Sys.setenv(LANGUAGE="de") library(Rdym) rmorm(10) # Fehler: konnte Funktion "rmorm" nicht finden # # Meinten Sie: rnorm ? # rnorm(10)
If your natural language is supported by R but does not work properly with Rdym, please file a report. In your report, please include how you are interfacing to R (e.g., RStudio, the terminal, R.app, ...) and the output of:
All of the "did you mean?" localizations (found here) were probably generated by Google translate, and may not make any sense to a native speaker. If you have suggestions for improvement, please consider submitting a pull request or opening an issue.