Wednesday, August 17, 2022

[FIXED] How to save verbose output

Issue

I would like to save the the verbose output of whatever R function to either a variable or file.

In other words, the verbose console output of whatever_R_function(abc, verbose=TRUE) should be saved somewhere.

I tried to play with verbose.output <- capture.output(whatever_R_function(abc, verbose = TRUE)) but it doesn't work as capture.output() captures the non-verbose part of the output only.

Two examples:

install.packages('devtools', verbose=TRUE)

or

library(emayili)
smtp <- server(host = '...',
               port = ...,
               username = '...',
               password = '...')
email <- envelope() %>%
    from('...') %>%
    to('...') %>%
    bcc('...') %>%
    reply('...') %>%
    subject('...') %>%
    html('...') %>%
    attachment('...')
smtp(email, verbose = TRUE)

Thank you.

R 4.0.2 - RStudio 1.3.1093 - macOS 10.15.7


Solution

I didn't dig in to the install.packages code, but smtp appears to use cat directed to stderr() when verbose = TRUE.

The ?capture.output help page says:

Messages sent to stderr() (including those from message, warning and stop) are captured by type = "message". Note that this can be “unsafe” and should only be used with care.

So, I believe if you use capture.output(..., type = "message"), you should get it. There's a strong possibility that this will work for install.packages too.

I'm not sure why this is considered unsafe or what care you should take with it...



Answered By - Gregor Thomas
Answer Checked By - Timothy Miller (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.