Replies: 1 comment 4 replies
-
Hello. TL;DRThe following uses best practices in R to capture output sent to standard output and messages to one file per iteration:
Personally, I would tidy it up by using a helper function that I can reuse, e.g.
Note that this is the approach you should take regardless of parallelization or not, e.g. with y <- list()
for(i in 1:2) {
redirect_stdout_and_messages_to_file(paste0('testout', i, '.log'), {
nimble::messageIfVerbose('nimble message') # message from Nimble
message('message message')
cat('cat message\n')
})
y[[i]] <- i
} DetailsThere are a few things going on in your original example. I'll try to clarify what's going, that things are working as expected and by design, and how you can get what you want. For a started, yes, correct res <- tryCatch(nimble::messageIfVerbose('nimble message'), condition = identity)
str(res)
#> List of 2
#> $ message: chr "nimble message\n"
#> $ call : language message(...)
#> - attr(*, "class")= chr [1:3] "simpleMessage" "message" "condition" So, in that sense, and for the discussion here, Second, you can think of Third, output from Fourth, the correct way to capture messages is not analogue to howe we capture standard output. That is, we do not want to use |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm calling the package
Nimble
from aforeach()
loop with%dofuture%
. Nimble outputs some messages, which I'd like to be redirected to log files, together with other messages generated withcat()
. Nimble seems to be using themessage()
function, here is its codeI try to achieve this by opening a connection to the log file and redirecting
sink()
there. This approach worked with thedoParallel
backend, but thefuture
backend keeps on sending the messages from Nimble ormessage()
to the main R session.Is this a bug? Or should I approach my goal differently?
Example with
future
:in this case the "cat message" correctly appears in each
testout....log
file, but the "nimble message" and the output frommessage()
are displayed in the main R session.Compare with
doParallel
:In this case all three messages appear in the log file.
I would expect the code with
future
to behave in a similar way as the one withdoParallel
, redirecting both messages to the log file.Session information
Beta Was this translation helpful? Give feedback.
All reactions