【问题标题】:How can I send the output of R CMD check to a file or variable?如何将 R CMD 检查的输出发送到文件或变量?
【发布时间】:2015-09-23 22:28:02
【问题描述】:

上下文:试图有效地解决我的no visibile binding CMD 检查问题。

captureOutput 的两种风格我都试过了:

cmd_output <- R.utils::captureOutput(devtools::check(args="--no-tests"))
cmd_output <- utils::capture.output(devtools::check())

下沉

sink("cmd_output.txt")
devtools::check(args="--no-tests")
sink()
unlink("cmd_output.txt")

无济于事。当我想要 CMD NOTEs 和 WARNINGs 时,我最终得到了 devtools::document() 的输出。

【问题讨论】:

  • 这与此处的问题相同:github.com/hadley/devtools/issues/315。基本上,devtools 使用system,它没有提供灵活的接口来控制输出,所以你可能不走运。使用system2 调用R CMD check 来解决此问题,或直接调用工具中的质量控制功能。
  • 非常有帮助 - 谢谢 - 你有没有机会建议通过 system2 调用 R CMD 检查的语法?证明对谷歌来说非常抽象。 system2("R", args = c("CMD", "check", "something goes here?"))

标签: r devtools cran


【解决方案1】:

这是我在构建和检查包时遵循的非开发工具工作流程。请注意,必须对 tarball,而不是包目录执行检查,因此您必须先构建。 devtools 以无形的方式执行此操作,但您必须明确说明。

假设您位于包含包目录的目录中(即,包目录的上一级)。

pkg <- "packagename"
version <- read.dcf(paste('.',pkg,'DESCRIPTION',sep='/'))[,'Version']
# build
system2("R", paste0("CMD build ", pkg), stdout = "build.txt", stderr = "build.txt")
# check
system2("R", paste0("CMD check ", pkg, "_", version, ".tar.gz --as-cran"), stdout = "check.txt", stderr = "check.txt")
# install
system2("R", paste0("CMD INSTALL -l ",Sys.getenv('R_HOME'),"/library ", pkg), stdout = "install.txt", stderr = "install.txt")

这会将每个命令的输出转储到其自己的文本文件中。

您还可以指定 wait = FALSE 以在单独的进程中运行这些,而不会占用您的 R。如果您的检查需要很长时间,这可能会很有用,因为您可以在 R 中继续其他工作。

Hadley 还建议,如果您使用check(),您可以访问由R CMD check 自动生成的检查文件,该文件位于此处:

chk <- check()
# install log
file.path(chk, paste0(as.package(".")$package, ".Rcheck"), "00install.out")
# check log
file.path(chk, paste0(as.package(".")$package, ".Rcheck"), "00check.log")

这样可能更不方便。

【讨论】:

    【解决方案2】:

    可能并非在所有情况下都有用,但一个快速简单的解决方案是简单地 reprex() check() 的输出:

    check_output <- reprex::reprex(devtools::check(), wd = ".")
    

    请注意,如果您不希望输出以 "#&gt;" 为前缀,则可以使用 comment = ""

    编辑: 我在尝试解决同样的问题时找到了这个解决方案 - 有效地解决了“无可见绑定”警告。对于其他想要做同样事情的人,这里有一个函数可以提取变量的字符向量,可以调用globalVariables()

    # Helper function to get global variables to specify before running devtools::check()
    get_missing_global_variables <- function(wd = getwd()) {
      
      # Run devtools::check() and reprex the results
      check_output <- reprex::reprex(input = sprintf("devtools::check(pkg = '%s', vignettes = FALSE)\n", wd), 
                                     comment = "")
      
      # Get the lines which are notes about missing global variables, extract the variables and 
      # construct a vector as a string
      missing_global_vars <- check_output %>% 
        stringr::str_squish() %>% 
        paste(collapse = " ") %>% 
        stringr::str_extract_all("no visible binding for global variable '[^']+'") %>% 
        `[[`(1) %>% 
        stringr::str_extract("'.+'$") %>%
        stringr::str_remove("^'") %>%
        stringr::str_remove("'$") %>%
        unique() %>%
        sort()
      
      # Get a vector to paste into `globalVariables()`
      to_print <- if (length(missing_global_vars) == 0) {
        "None" 
      } else { 
        missing_global_vars %>% 
          paste0('"', ., '"', collapse = ", \n  ") %>% 
          paste0("c(", ., ")")
      }
      
      # Put the global variables in the console
      cat("Missing global variables:\n", to_print)
      
      # Return the results of the check
      invisible(missing_global_vars)
      
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-04
      • 1970-01-01
      • 2014-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多