我正在开发一个R包,其中一个函数包含match.fun对导入到包命名空间的包中的函数的调用.但是在加载包时,match.fun调用找不到函数名.从Hadley Wickham的描述我认为我做的一切都是正确的,但事实显然并非如此.
例:
# in the package file header, for creation of the NAMESPACE via roxygen2:
##` @import topicmodels
# The function declaration in the package
ModelTopics <- function(doc.term.mat, num.topics, topic.method="LDA"){
topic.fun <- match.fun(topic.method)
output <- topic.fun(doc.term.mat, k=num.topics)
return(output)
}
Run Code Online (Sandbox Code Playgroud)
然后在R:
> library(mypackage)
> sample.output <- ModelTopics(my.dtm, topic.method="LDA", num.topics=5)
Error in get(as.character(FUN), mode = "function", envir = envir) :
object 'LDA' of mode 'function' was not found
Run Code Online (Sandbox Code Playgroud)
根据我对命名空间的理解,match.fun调用应该可以访问包命名空间,包括topicmodels函数.但这似乎并非如此.如果我topicmodels直接导入R会话的全局命名空间,那么这是有效的. …