rcommand-line-interface

Why is the order of CLI messages different from the code in R


I have come across rather weird behavior when using the cli package to print Command Line messages. Please see the simple example below:

library(cli)


test_cli_orders <- function() {
  
  Sys.sleep(1)
  
  cli_progress_step(msg = "Doing 1", msg_done = "1 is done")
  
  Sys.sleep(1)
  
  cli_progress_step(msg = "Doing 2", msg_done = "2 is done")
  
  Sys.sleep(1)
  
  cli_text("I should be displayed last...")
  
}

When running this function,

test_cli_orders()

I'd expect the text of cli_text to appear after "2 is done" from the last cli_progress_step, but it yields:

enter image description here

It is very confusing, and I have no idea what caused it after many experiments. I'd greatly appreciate your thoughts.


Solution

  • This is because your cli_text at the end is considered as a progress of previous cli_progress. You can end it by cli_progress_done before printing your text.

    library(cli)
    
    
    test_cli_orders <- function() {
      
      Sys.sleep(1)
      
      cli_progress_step(msg = "doing 1", msg_done = "1 is done")
      
      Sys.sleep(1)
      
      cli_progress_step(msg = "Doing 2", msg_done = "2 is done")
      
      Sys.sleep(1)
      
      cli_progress_done()
      
      cli_text("I should be displayed last...")
      
    }
    
    test_cli_orders()
    

    RESULTS