script progress bar in R, purrr progress bar, lapply progress bar

Script progress bar in R that works with purrr and in batch mode

Here is how to create a progress bar in R that is versatile when working with loops, for example, by using package purrr or the function lapply.
Sometimes there is also necessary to see progress when running R scripts in batch mode from the command line and remove unnecessary messages.

 

First of all, to make it cleaner when the script is executed in batch mode, take a look at this post to know how to hide R package loading information and unnecessary warnings.

 

Create script progress bar in R

To create a progress bar in R I will use the capabilities of package progressr.

For me, it is versatile enough to get the result exactly as I want and see the progress bar while running the script in RStudio or batch mode. Meanwhile, there are also very good-looking progress bars from the cli package.

Here is a simple progress bar with the function apply.

options(progressr.clear = FALSE)

pacman::p_load(progressr)


my_fcn <- function(xs) {
  p <- progressor(along = xs)
  
  y <- lapply(xs, function(x) {
    Sys.sleep(0.1)
    p(sprintf("x=%g", x))
    
    sqrt(x)
  })
}


with_progress(my_fcn(1:50))

Keep the R progress bar visible after the execution

If you want to keep the progress bar from the progressr package in the console after it reaches 100% here is how to do that. Add this option at the beginning of the script.

options(progressr.clear = FALSE)

 

Progress bar for purrr

Here is the progress bar for the function map_df from the purrr package that I used in the previous post to combine Excel files.

options(progressr.clear = FALSE)

pacman::p_load(progressr, purrr, dplyr, readxl)


# collecting Excel file paths

xlsxfiles <-
  list.files(
    path = "C:/Users/datacornering/Downloads/MultiFiles",
    pattern = "*.xlsx",
    full.names = TRUE,
    recursive = TRUE
  )


# map_df loop

my_fcn <- function(xs) {
  p <- progressor(along = xs)
  
  y <- map_df(xlsxfiles, function(x) {
  Sys.sleep(0.1)
  p(sprintf("x=%s", x))
    
    read_excel(x) %>%
    mutate("Source" = basename(x),
           "Mod DT" = file.info(x)$mtime)
  })
}


# combining Excel files in data frame "result"
# while showing the progress bar

with_progress(result <- my_fcn(xlsxfiles))

I’m also ensuring that the result is written in the data frame.

 

Create script progress bar for batch mode in R

To run the script and see the progress bar in the command line you should set an additional option at the beginning. You can do that by using with option progress.enable like this.

options(progressr.enable = TRUE)

It is also possible to set the environmental variable.

Sys.setenv(R_PROGRESSR_ENABLE = TRUE)

Here is how it looks in a combination with the function lapply loop progress bar.

options(progressr.enable = TRUE)
options(progressr.clear = FALSE)


pacman::p_load(progressr)


my_fcn <- function(xs) {
  p <- progressor(along = xs)
  
  y <- lapply(xs, function(x) {
    Sys.sleep(0.1)
    p(sprintf("x=%g", x))
    
    sqrt(x)
  })
}


with_progress(my_fcn(1:50))

At the end of the script, you can create a notification that is printed instead of showing the full progress bar.

print("done!", quote = FALSE)

 

The progress bar works perfectly if you are using R script as a source.

 

Create the progressing sound in R

Package beepr is useful to play a sound when running an R script. By using function handlers you can configure how your progress bar will look and even sound.

For example, if it is necessary to see not only the progress bar but hear a sound of the progress you can use multiple handlers. It is also possible to configure them by using this vignette.

options(progressr.enable = TRUE)
options(progressr.clear = FALSE)

pacman::p_load(progressr)


handlers(c("txtprogressbar", "beepr"))

my_fcn <- function(xs) {
  p <- progressor(along = xs)
  
  y <- lapply(xs, function(x) {
    Sys.sleep(0.1)
    p(sprintf("x=%g", x))
    
    sqrt(x)
  })
}



with_progress(my_fcn(1:50))

Sound is also working if the script is executed from the command line. If you want to play synthesized voice after R script execution here is how to do that on Windows.

 

I hope it helps you track progress in R script in an easy-to-view manner.