rprogress-bar

How to correctly use `progress::progress_bar$message()`?


I am trying to wrap my head around the progress package to make nice progress bars. I can make the basic examples work, but I cannot figure out how to use the progress_bar$message() feature to print custom messages above the progress bar. I could find examples neither in the documentation nor on Google or Stackoverflow. The documentation only summarily describes it as such:

progress_bar$message() prints a message above the progress bar. It fails if the progress bar has already finished.

Here are my various attempts to make this work (reprex):

library(progress)
#> Warning: package 'progress' was built under R version 4.4.0

# First example just to show that it works
pb <- progress_bar$new(total = 100)
for (i in 1:100) {
  pb$tick()
  Sys.sleep(1 / 100)
}

# Following examples don't work
pb <- progress_bar$new(total = 100)
for (i in 1:100) {
  pb$tick()
  Sys.sleep(1 / 100)
  progress_bar$message("test")
  Sys.sleep(1 / 100)
}
#> Error in progress_bar$message("test"): attempt to apply non-function

pb <- progress_bar$new(total = 100)
for (i in 1:100) {
  pb$tick()
  Sys.sleep(1 / 100)
  progress_bar$message()
  Sys.sleep(1 / 100)
}
#> Error in progress_bar$message(): attempt to apply non-function

pb <- progress_bar$new(total = 100)
for (i in 1:100) {
  pb$tick()
  Sys.sleep(1 / 100)
  progress_bar$message(print())
  Sys.sleep(1 / 100)
}
#> Error in progress_bar$message(print()): attempt to apply non-function

In sum, I am always getting the "attempt to apply non-function" error, but it is not clear to me what I actually need to do.

Created on 2024-05-16 with reprex v2.1.0


Solution

  • Ok, let me provide my comment as an answer then to get this closed:

    message() is a method provided by the c("progress_bar", "R6") class, pb is your specific object of this class, c.f. class(pb).

    Therefore, the syntax to call said method is pb$message("test").