Gilead-BioStats / gsm

Good Statistical Monitoring R Package
https://gilead-biostats.github.io/gsm/
Apache License 2.0
39 stars 9 forks source link

Add hooks to `RunWorkflow`. #1961

Open samussiah opened 3 days ago

samussiah commented 3 days ago

Overview

Adds a LoadData hook at the beginning of RunWorkflow and a SaveData hook at the end.

Test Notes/Sample Code

Simplest use case that hits clindata and writes to .csv, also captured in inst/examples/WorkflowIO.R:

load_all()

LoadData <- function(lWorkflow, lConfig) {
    purrr::imap(
        lWorkflow$spec,
        ~ {
            input <- lConfig$Domains[[ .y ]]

            if (is.data.frame(input)) {
                data <- input
            } else if (is.function(input)) {
                data <- input()
            } else if (is.character(input)) {
                data <- read.csv(input)
            } else {
                cli::cli_abort("Invalid data source: {input}.")
            }

            return(ApplySpec(data, .x))
        }
    )
}

SaveData <- function(lWorkflow, lConfig) {
    domain <- paste0(lWorkflow$meta$Type, '_', lWorkflow$meta$ID)
    cli::cli_alert_info(domain)

    if (exists(domain, lConfig$Domains)) {
        output <- lConfig$Domains[[ domain ]]
        cli::cli_alert_info(output)

        cli::cli_alert_info(
            'Saving output of `lWorkflow` to `{output}`.'
        )

        write.csv(
            lWorkflow$lResult,
            output
        )
    } else {
        cli::cli_alert_info(
            '{domain} not found.'
        )
    }
}

lConfig <- list(
    LoadData = LoadData,
    SaveData = SaveData,
    Domains = list(
        Raw_PD = function() { clindata::ctms_protdev },
        Raw_SUBJ = function() { clindata::rawplus_dm },
        Raw_SITE = function() { clindata::ctms_site },

        Mapped_PD = paste0(tempdir(), '/mapped-pd.csv'),
        Mapped_SUBJ = paste0(tempdir(), '/mapped-subj.csv'),
        Mapped_SITE = paste0(tempdir(), '/mapped-site.csv')
    )
)

lMappedData <- RunWorkflows(
    MakeWorkflowList(c('SUBJ', 'PD', 'SITE')),
    lConfig = lConfig
)

Connected Issues

samussiah commented 18 hours ago

Still working on unit tests @lauramaxwell but the functionality is there.

lauramaxwell commented 18 hours ago

Still working on unit tests @lauramaxwell but the functionality is there.

Nice, @samussiah - took a quick look and everything in the example worked as i would expect. I'll dig in a bit more in the morning, but so far so good! let me know if you want me to look into the tests, etc.