包中建议使用 RDCOMClient

And*_*ell 5 r package rdcomclient

我通过描述文件使用 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)
}
Run Code Online (Sandbox Code Playgroud)

如今,这似乎被认为是不好的做法,“编写 R 扩展,1.1.3.1 建议的包”告诉我们要制定:

if (requireNamespace("rgl", quietly = TRUE)) {
   rgl::plot3d(...)
} else {
   ## do something else not involving rgl.
}
Run Code Online (Sandbox Code Playgroud)

或者: ..如果想要在建议的包不可用时给出错误,只需使用例如 rgl::plot3d。

重新编码(根据我的理解)意味着,只需删除要求语句:

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)
}
Run Code Online (Sandbox Code Playgroud)

这样做会导致以下运行时错误:

Error in RDCOMClient::COMCreate("Word.Application", existing = FALSE) :
  could not find function "createCOMReference"
Run Code Online (Sandbox Code Playgroud)

createCOMReference 是 RDCOMClient 中的一个函数,如果没有显式的 require 语句,显然无法找到该函数。

看在上帝的份上,我应该如何将 RDCOMClient 集成到我的包中以符合 CRAN 的政策???

tho*_*hal 1

很老的问题,但我偶然发现了同样的问题。我使用以下解决方法,它不会发出警告,R CMD check但在我看来是一个可怕的黑客:

if (requireNamespace("RDCOMClient", quietly = TRUE)) {
   if (!"RDCOMClient" %in% .packages()) {
      attachNamespace("RDCOMClient")
   }
# ...
}
Run Code Online (Sandbox Code Playgroud)