在R中抑制安装输出

时间:2012-09-13 23:09:19

标签: r suppressmessage

这真的开始让我感到烦恼......我尝试了一些方法,但似乎都没有工作

我正在从一个函数运行一个安装,它生成了许多我想要抑制的不必要的消息,但是我尝试这样做的所有方法都没有用。

我想要压缩的代码是:install_github('ROAUth', 'duncantl'),它需要预先加载包devtools

无论如何,我尝试了invisiblecapture.outputsink,其中没有一个可行......或者我可能没有正确使用它们......无论哪种方式......任何想法?

3 个答案:

答案 0 :(得分:10)

suppressMessages会关闭一些消息(通过调用message打印的消息),但不是全部。

其余消息来自R CMD INSTALL通过system2功能调用sink。我认为这是因为你所尝试的所有常见事情(capture.outputsystem2等)无效。请注意,stderr函数附带stdoutFALSE个参数,如果转到system2,则会关闭所有这些消息。不幸的是,stdout = ""默认使用stderr = ""devtools,似乎无法通过system2包访问这些参数。

因此,我设法在没有任何消息的情况下运行的一种方法是临时覆盖基本环境中的# store a copy of system2 assign("system2.default", base::system2, baseenv()) # create a quiet version of system2 assign("system2.quiet", function(...)system2.default(..., stdout = FALSE, stderr = FALSE), baseenv()) # overwrite system2 with the quiet version assignInNamespace("system2", system2.quiet, "base") # this is now message-free: res <- eval(suppressMessages(install_github('ROAUth', 'duncantl'))) # reset system2 to its original version assignInNamespace("system2", system2.default, "base") 函数。它不是特别优雅,但它有效:

{{1}}

答案 1 :(得分:3)

这是另一种可能性。这样做的好处是,您无需在致电system2后重置install_githubsystem2将继续展示所有来电的默认行为,但已启动致电install_github()

# store a copy of system2
assign("system2.default", base::system2, baseenv())

# create a quiet version of system2
assign("system2.quiet", function(...)system2.default(..., stdout = FALSE,
                                                     stderr = FALSE), baseenv())
# redefine system2 to use system2.quiet if called from "install_github"
assignInNamespace("system2",
    function(...) {
        cls <- sys.calls()
        from_install_github <- 
            any(sapply(cls, "[[", 1) == as.name("install_github"))
        if(from_install_github) {
            system2.quiet(...)
        } else {
            system2.default(...)
        }},
    "base")


## Try it out
library(devtools)
suppressMessages(install_github('ROAUth', 'duncantl'))

答案 2 :(得分:2)

另一种技术是修补devtools函数,以便它们允许您将stdout参数传递给system2。也不是很优雅,但也许你可以说服包作者以这种方式修改devtools。以下是我修补的buildinstall函数:

library(devtools)

# New functions.
my.install<-function (pkg = ".", reload = TRUE, quick = FALSE, args = NULL, ...) 
{
    pkg <- as.package(pkg)
    message("Installing ", pkg$package)
    devtools:::install_deps(pkg)
    built_path <- devtools:::build(pkg, tempdir(),...) # pass along the stdout arg
    on.exit(unlink(built_path))
    opts <- c(paste("--library=", shQuote(.libPaths()[1]), sep = ""), 
        "--with-keep.source")
    if (quick) {
        opts <- c(opts, "--no-docs", "--no-multiarch", "--no-demo")
    }
    opts <- paste(paste(opts, collapse = " "), paste(args, collapse = " "))
    devtools:::R(paste("CMD INSTALL ", shQuote(built_path), " ", opts, sep = ""),...) # pass along the stdout arg
    if (reload) 
        devtools:::reload(pkg)
    invisible(TRUE)
}

my.build<-function (pkg = ".", path = NULL, binary = FALSE, ...) 
{
    pkg <- as.package(pkg)
    if (is.null(path)) {
        path <- dirname(pkg$path)
    }
    if (binary) {
        cmd <- paste("CMD INSTALL ", shQuote(pkg$path), " --build", 
            sep = "")
        ext <- if (.Platform$OS.type == "windows") 
            "zip"
        else "tgz"
    }
    else {
        cmd <- paste("CMD build ", shQuote(pkg$path), " --no-manual --no-resave-data", 
            sep = "")
        ext <- "tar.gz"
    }
    devtools:::R(cmd, path, ...) # pass along the stdout arg
    targz <- paste(pkg$package, "_", pkg$version, ".", ext, sep = "")
    file.path(path, targz)
}

# Patch package.
unlockBinding("install", as.environment("package:devtools"))
unlockBinding("build", as.environment("package:devtools"))
assignInNamespace('install', my.install, ns='devtools', envir=as.environment("package:devtools"));
assignInNamespace('build', my.build, ns='devtools', envir=as.environment("package:devtools"));
lockBinding("install", as.environment("package:devtools"))
lockBinding("build", as.environment("package:devtools"))

# Run with no messages.
suppressMessages(install_github('ROAUth','duncantl',stdout=NULL))

基本上,您在三个地方传递...,在install函数中传递两次,在build函数中传递一次。