我正在开发一个 R 包。这是我的 S3 类和相关函数。
# Define a generic function for getting suggestions
get_suggestion <- function(x, language) {
# Define a new S3 class for misspelled words
setClass("misspelled_words", representation(words="character"))
UseMethod("get_suggestion")
}
# Define a method for character vectors
get_suggestion.character <- function(misspelled_words, language) {
suggestions <- hunspell::hunspell_suggest(misspelled_words, dict = dictionary(language))
message(paste0("Getting suggestions using ", language, " dictionary."))
return(suggestions)
}
# Define a method for misspelled_words objects
get_suggestion.misspelled_words <- function(misspelled_words, language) {
suggestions <- hunspell::hunspell_suggest(misspelled_words@words, dict = dictionary(language))
message(paste0("Getting suggestions using ", language, " dictionary."))
return(suggestions)
}
# Define a method for lists of misspelled_words objects(using parallel processing)
get_suggestion.list <- function(misspelled_words, language) {
cl <- parallel::makeCluster(parallel::detectCores()-1)
doParallel::registerDoParallel(cl)
suggestions <- foreach::foreach(mw = misspelled_words, .combine = c) %dopar% {
hunspell::hunspell_suggest(mw, language)
}
parallel::stopCluster(cl)
return(suggestions)
}
我在测试示例上运行这个函数时没有错误。但是当我把这些函数放到我的包中,我加载我的包,运行测试示例时,我遇到了
Error in assign(mname, def, where) : cannot add bindings to a locked environment
想过解锁环境,但是好像很危险。有没有办法解决这个错误?