R progress bar not captured in log file when directed from bash
NLBOKE1 opened this issue · 2 comments
In linux, from bash, when I am re-routing the output of an R code (in non-interactive mode, and I took care of the environment variable progressr enable, see below) which contains a progress bar (from progressr package), the progress bar is not captured in the log file (i am monitoring the log file with tail -f output.log
). I need this setup to work as one of my applications which executes R codes is monitoring these logs files and displays them in its GUI.
Below is the code I have tested in centos 7 (R 4.0.2). This code is in a file called test.R.
require(progress)
require(progressr)
slow_sum <- function(x) {
p <- progressr::progressor(along = x)
sum <- 0
for (kk in seq_along(x)) {
Sys.sleep(1)
sum <- sum + x[kk]
p(message = sprintf("Adding %g", x[kk]))
}
sum
}
progressr::handlers(
list(progressr::handler_progress(
format = ":spin :current/:total (:message) -- [:bar] :percent in :elapsed ETA: :eta\r",
width = 60,
complete = "+")))
progressr::with_progress(y <- slow_sum(1:10))
Then when I try to execute this code as follows from terminal
export R_PROGRESSR_ENABLE=TRUE; R --no-save --args < ~/test.R >> output.log 2>&1
and monitor the progress using tail -f output.log
, I dont get any progress bar updates in the log file. I realised that if I use default handler (by removing the progressr:handlers call) then it works just fine...
Apparently each handler also requires enable = TRUE, when I changed to list(progressr::handler_progress(enable = TRUE, ...
things started working just fine.
You can also set R option
options(progressr.enable = TRUE)
or environment variable
R_PROGRESSR_ENABLE=true
in your shell script, cf. https://progressr.futureverse.org/reference/progressr.options.html