我通过描述文件使用 RDCOMClient 获得了我的 R 包:
建议:RDCOMClient
以及以下(完美工作)代码:
GetNewWrd <- function() {
stopifnot(require(RDCOMClient))
# Starts the Word application with wrd as handle
wrd <- RDCOMClient::COMCreate("Word.Application", existing=FALSE)
newdoc <- wrd[["Documents"]]$Add("",FALSE, 0)
wrd[["Visible"]] <- TRUE
invisible(wrd)
}
现在这似乎被认为是不好的做法,“Writing R Extensions, 1.1.3.1 Suggested packages”告诉我们制定:
if (requireNamespace("rgl", quietly = TRUE)) {
rgl::plot3d(...)
} else {
## do something else not involving rgl.
}
或: .. 如果打算在建议的包不可用时给出错误,只需使用例如rgl::plot3d。
重新编码(据我所知)意味着,只需删除 require 语句:
GetNewWrd <- function() {
# Starts the Word application with wrd as handle
wrd <- RDCOMClient::COMCreate("Word.Application", existing=FALSE)
newdoc <- wrd[["Documents"]]$Add("",FALSE, 0)
wrd[["Visible"]] <- TRUE
invisible(wrd)
}
这样做会导致以下运行时错误:
Error in RDCOMClient::COMCreate("Word.Application", existing = FALSE) :
could not find function "createCOMReference"
createCOMReference 是 RDCOMClient 中的一个函数,如果没有明确的 require 语句,显然无法找到它。
看在上帝的份上,我应该如何按照 CRAN 的政策将 RDCOMClient 集成到我的包中???
最佳答案
很老的问题,但我偶然发现了同样的问题。我使用以下解决方法,它不会在 R CMD check
中发出警告但在我看来就像一个可怕的黑客:
if (requireNamespace("RDCOMClient", quietly = TRUE)) {
if (!"RDCOMClient" %in% .packages()) {
attachNamespace("RDCOMClient")
}
# ...
}
关于包中建议的 RDCOMClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27910886/