billdenney/pknca

Add an optional progress indicator

john-harrold opened this issue · 5 comments

Would it be possible to add an optional progress indicator for both terminal and shiny apps? I can provide examples of how to do that for shiny apps if it's not apparent.

It may be possible. Currently, the calculations are done within a purrr::pmap call (more accurately one for dense PK and one for sparse PK). I used to have it run in parallel there, but there are cross-platform compatibility issues with the way I used to do it with mclapply() (it dropped warnings on operating systems other than Windows, https://bugs.r-project.org/show_bug.cgi?id=17122). Long-term, I'd like to go back to parallel execution.

Do you see how to add it within the code below? That is the most time-intense part, I think (based on code profiling a long time ago).

pknca/R/pk.calc.all.R

Lines 44 to 73 in 46a7091

results_dense <-
purrr::pmap(
.l=list(
data_conc=splitdata$data_conc,
data_dose=splitdata$data_dose,
data_intervals=splitdata$data_intervals
),
.f=pk.nca.intervals,
options=data$options,
impute=data$impute,
verbose=verbose,
sparse=FALSE
)
if (verbose) message("Combining completed dense PK calculation results.")
results <- pk_nca_result_to_df(group_info, results_dense)
if (is_sparse_pk(data)) {
if (verbose) message("Starting sparse PK NCA calculations.")
results_sparse <-
purrr::pmap(
.l=list(
data_conc=splitdata$data_sparse_conc,
data_dose=splitdata$data_dose,
data_intervals=splitdata$data_intervals
),
.f=pk.nca.intervals,
options=data$options,
impute=data$impute,
verbose=verbose,
sparse=TRUE
)

@john-harrold, If I added a PKNCA.options() value for progress_bar that would be default to be FALSE and would be passed to this pmap call, would that cover your use case? I don't know how to expose this progress bar in shiny.

I think the purrr 1.0 adds a progress bar, in case you were wondering, I'm assuming you already know.

I started creating examples using conditionals to switch between Shiny and the console. Then I read and realized that the progress indicator in the cli package will work in Shiny. I read through the purr documentation, and it says it's using cli for the progress indicator. Which makes sense because they are both Hadley associated packages :).

The TLDR; is that progress indicators in purr will probably work in Shiny without any changes necessary. To test that you can use the example below. It will show a progress indicator when you click on the button. In the server function you can comment out the do_something_cli() and put your PKNCA code that produces a progress indicator and see if that works.

library(cli)
do_something_cli = function(ninc=10){
  
  # Starting the progress bar
    pb = cli_progress_bar("Running NCA", total = ninc) 
  # doing something:
  for(i in 1:ninc){
    Sys.sleep(1.0)
    # Updating the progress bar
     cli::cli_progress_update(id=pb)
  }
 # Closing the progress bar 
   cli::cli_progress_done(id=pb)
  
}

library(shiny)

ui <- fluidPage(

    # Application title
    titlePanel("Progress Bar"),
    actionButton("button", "Show progress bar")
)

server <- function(input, output) {
  observeEvent(input$button, {
    # Puth the PKNCA code that uses a progress bar here
    do_something_cli()
  })
}

# Run the application
shinyApp(ui = ui, server = server)

PKNCA.options(progress = TRUE) now shows a progress bar (showing the bar is the default).