Skip to content

Commit

Permalink
Add progress bar to getClinicalTrials (#85)
Browse files Browse the repository at this point in the history
This adds a progress bar, showing the percentage of trials simulated as
part of getClinicalTrials().
  • Loading branch information
holgstr authored Nov 7, 2023
1 parent fda7e69 commit 7bd34ae
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
20 changes: 17 additions & 3 deletions R/getClinicalTrials.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,22 @@ getDatasetWideFormat <- function(data) {
newdata
}

#' Helper Function for Adding Progress Bar to Trial Simulation
#'
#' @param x (`int`)\cr iteration index within lapply.
#' @param ... parameters transferred to [getOneClinicalTrial()], see [getOneClinicalTrial()] for details.
#'
#' @return This returns the same as [getOneClinicalTrial()], but updates the progress bar.
#' @keywords internal
runTrial <- function(x, pb, ...) {
utils::setTxtProgressBar(pb, x)
getOneClinicalTrial(...)
}

#' Simulation of a Large Number of Oncology Clinical Trials
#'
#' @param nRep (`int`)\cr number of simulated trials.
#' @param ... parameters transferred to [getOneClinicalTrial()], see [getOneClinicalTrial()] for details.
#' @param ... parameters transferred to [getOneClinicalTrial()], see [getOneClinicalTrial()] for details.
#' @param seed (`int`)\cr random seed used for this simulation.
#' @param datType (`string`)\cr possible values are `1rowTransition` and `1rowPatient`.
#'
Expand All @@ -175,13 +187,15 @@ getClinicalTrials <- function(nRep, ..., seed = 1234, datType = "1rowTransition"
assert_choice(datType, c("1rowTransition", "1rowPatient"))

set.seed(seed)
cat("Simulating", nRep, "trials:\n")
pb <- utils::txtProgressBar(min = 0, max = nRep, style = 3)
# getOneClinicalTrial generates a single clinical trial with multiple arms. Generate nRep simulated trials:
simulatedTrials <- lapply(
seq_len(nRep),
FUN = function(x, ...) getOneClinicalTrial(...),
FUN = function(x, ...) runTrial(x, pb, ...),
...
)

close(pb)
# Final data set format: one row per patient or one row per transition?
if (datType == "1rowPatient") {
simulatedTrials <- lapply(simulatedTrials, getDatasetWideFormat)
Expand Down
1 change: 1 addition & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ entryAct
exitAct
funder
integrand
lapply
multistate
pre
recruitTime
Expand Down
2 changes: 1 addition & 1 deletion man/getClinicalTrials.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions man/runTrial.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7bd34ae

Please sign in to comment.