如何将R CMD check的输出发送到文件或变量?

如何将R CMD check的输出发送到文件或变量?,r,devtools,cran,R,Devtools,Cran,上下文:尝试有效解决我的CMD检查问题 我尝试了两种风格的captureOutput: cmd_output <- R.utils::captureOutput(devtools::check(args="--no-tests")) cmd_output <- utils::capture.output(devtools::check()) 无济于事。当我需要CMD注释和警告时,我最终得到了devtools::document()的输出。以下是我在构建和检查包时遵循的非devtoo

上下文:尝试有效解决我的CMD检查问题

我尝试了两种风格的
captureOutput

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

无济于事。当我需要CMD注释和警告时,我最终得到了
devtools::document()
的输出。

以下是我在构建和检查包时遵循的非devtools工作流。请注意,必须在tarball上执行检查,而不是在包目录上执行检查,因此必须首先构建。devtools以不可见的方式执行此操作,但您必须将其显式化

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


这可能更方便。

可能并非在所有情况下都有用,但一个简单快捷的解决方案是只需
reprex()
检查()的输出即可:


这与此处的问题相同:。基本上,devtools使用的是
系统
,它没有提供一个灵活的接口来控制输出,所以您可能运气不好。使用
system2
调用
R CMD check
来解决这个问题,或者直接调用tools中的质量控制函数。非常有用-谢谢-有没有可能建议通过system2调用R CMD check的语法?这对谷歌来说是非常抽象的<代码>系统2(“R”,args=c(“CMD”,“check”,“这里有东西吗?”)
sink("cmd_output.txt")
devtools::check(args="--no-tests")
sink()
unlink("cmd_output.txt")
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")
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")
check_output <- reprex::reprex(devtools::check(), wd = ".")
# 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)
  
}