Search code examples
rdevtoolscran

How can I send the output of R CMD check to a file or variable?


Context: trying to efficiently resolve my no visibile binding CMD check problems.

I've tried both flavors of captureOutput:

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

and sink

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

to no avail. I end up with the output of devtools::document() when I want the CMD NOTEs and WARNINGs.


Solution

  • Here's the non-devtools workflow that I follow for building and checking a package. Note that checks have to be performed on the tarball, not the package directory, so you have to build first. devtools does this invisibly, but you have to make it explicit.

    Assume you're in the directory containing the package directory (i.e., one level up from the package directory).

    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")
    

    This will dump the output from each command into its own text file.

    You can also specify wait = FALSE to run these in a separate process without holding up your R. That might be useful if your checks take a long time because you can proceed with other work from within R.

    Hadley also suggested that if you use check() you could access the check file that is generated automatically by R CMD check, which would be located here:

    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")
    

    That may more may not be more convenient.