Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress install outputs in R

This is really starting to bug me...I have tried a few methods and none seem to work

I am running an install from a function which generates a lot of unnecessary messages that I would like to suppress, but all of the methods I tried to do this have not worked.

The bit of code I am trying to suppress is : install_github('ROAUth', 'duncantl'), it requires the package devtools to be loaded beforehand.

Anyway, I tried invisible, capture.output and sink, none of which work...or perhaps I am not using them correctly... either way...any ideas?

like image 461
h.l.m Avatar asked Sep 13 '12 23:09

h.l.m


People also ask

How do I suppress outputs in R?

By using invisible() function we can suppress the output.

How do I suppress Library messages in R?

suppressPackageStartupMessages() method in R language can be used to disable messages displayed upon loading a package in R. This method is used to suppress package startup messages. The package should be pre-installed in R, otherwise, a warning is displayed upon function call.


1 Answers

Another technique would be to patch the devtools functions so that they allow you to pass the stdout argument to system2. Also not very elegant, but perhaps you could convince the package authors to modify devtools in this way. Here are my patched build and install functions:

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

Essentially, you pass along the ... in three places, twice in the install function, and once in the build function.

like image 121
nograpes Avatar answered Oct 15 '22 06:10

nograpes