包中建议的 RDCOMClient

标签 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)
}

现在这似乎被认为是不好的做法,“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/

相关文章:

r - 纽约数据的 tract_choropleth

r - 计算两级因子交替时重新开始的差异

r - 使用 R 中的 RegEx 以 Mon、DD、YYYY 格式解析日期

r - 缺失数据时facet_grid 和尺度 ="free"的行为

delphi - 如何从 BPL 确定它是否仅限于设计时间

r - 通过 Outlook (RDCOMclient) 将 R Markdown 输出作为正文电子邮件发送

通过 RDCOMClient 从 R 运行 Excel 宏,错误 -2147418111

r - RDCOMClient错误-不支持InterfaceSupportsErrorInfo

python - 如何导入包内所有模块的成员?

maven - 如何使用 GitHub Actions 安装由 GitHub Packages 托管的包