R/alluvialsurvival.h.R

Defines functions alluvialSurvival

Documented in alluvialSurvival

# This file is automatically generated, you probably don't want to edit this

alluvialSurvivalOptions <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "alluvialSurvivalOptions",
    inherit = jmvcore::Options,
    public = list(
        initialize = function(
            timeVar = NULL,
            stageVar = NULL,
            treatmentVar = NULL,
            patientId = NULL,
            survivalVar = NULL,
            showRightAxis = FALSE,
            colorScheme = "clinical",
            showSurvival = FALSE, ...) {

            super$initialize(
                package="ClinicoPath",
                name="alluvialSurvival",
                requiresData=TRUE,
                ...)

            private$..timeVar <- jmvcore::OptionVariable$new(
                "timeVar",
                timeVar,
                suggested=list(
                    "continuous"),
                permitted=list(
                    "numeric"))
            private$..stageVar <- jmvcore::OptionVariable$new(
                "stageVar",
                stageVar,
                suggested=list(
                    "ordinal",
                    "nominal"),
                permitted=list(
                    "factor"))
            private$..treatmentVar <- jmvcore::OptionVariable$new(
                "treatmentVar",
                treatmentVar,
                suggested=list(
                    "ordinal",
                    "nominal"),
                permitted=list(
                    "factor"))
            private$..patientId <- jmvcore::OptionVariable$new(
                "patientId",
                patientId,
                suggested=list(
                    "nominal"),
                permitted=list(
                    "factor",
                    "id"))
            private$..survivalVar <- jmvcore::OptionVariable$new(
                "survivalVar",
                survivalVar,
                suggested=list(
                    "nominal"),
                permitted=list(
                    "factor"))
            private$..showRightAxis <- jmvcore::OptionBool$new(
                "showRightAxis",
                showRightAxis,
                default=FALSE)
            private$..colorScheme <- jmvcore::OptionList$new(
                "colorScheme",
                colorScheme,
                options=list(
                    "clinical",
                    "colorblind"),
                default="clinical")
            private$..showSurvival <- jmvcore::OptionBool$new(
                "showSurvival",
                showSurvival,
                default=FALSE)

            self$.addOption(private$..timeVar)
            self$.addOption(private$..stageVar)
            self$.addOption(private$..treatmentVar)
            self$.addOption(private$..patientId)
            self$.addOption(private$..survivalVar)
            self$.addOption(private$..showRightAxis)
            self$.addOption(private$..colorScheme)
            self$.addOption(private$..showSurvival)
        }),
    active = list(
        timeVar = function() private$..timeVar$value,
        stageVar = function() private$..stageVar$value,
        treatmentVar = function() private$..treatmentVar$value,
        patientId = function() private$..patientId$value,
        survivalVar = function() private$..survivalVar$value,
        showRightAxis = function() private$..showRightAxis$value,
        colorScheme = function() private$..colorScheme$value,
        showSurvival = function() private$..showSurvival$value),
    private = list(
        ..timeVar = NA,
        ..stageVar = NA,
        ..treatmentVar = NA,
        ..patientId = NA,
        ..survivalVar = NA,
        ..showRightAxis = NA,
        ..colorScheme = NA,
        ..showSurvival = NA)
)

alluvialSurvivalResults <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "alluvialSurvivalResults",
    inherit = jmvcore::Group,
    active = list(
        todo = function() private$.items[["todo"]],
        mydataview = function() private$.items[["mydataview"]],
        plot = function() private$.items[["plot"]],
        summaryTable = function() private$.items[["summaryTable"]],
        survivalStats = function() private$.items[["survivalStats"]]),
    private = list(),
    public=list(
        initialize=function(options) {
            super$initialize(
                options=options,
                name="",
                title="Treatment Pathway Alluvial Plot")
            self$add(jmvcore::Html$new(
                options=options,
                name="todo",
                title="To Do"))
            self$add(jmvcore::Preformatted$new(
                options=options,
                name="mydataview",
                title="mydataview"))
            self$add(jmvcore::Image$new(
                options=options,
                name="plot",
                title="Treatment Pathway Plot",
                width=800,
                height=600,
                renderFun=".plot"))
            self$add(jmvcore::Table$new(
                options=options,
                name="summaryTable",
                title="Summary Statistics",
                rows=0,
                clearWith=list(
                    "timeVar",
                    "stageVar",
                    "treatmentVar"),
                columns=list(
                    list(
                        `name`="timePoint", 
                        `title`="Time Point", 
                        `type`="number"),
                    list(
                        `name`="totalPatients", 
                        `title`="Total Patients", 
                        `type`="integer"),
                    list(
                        `name`="stageDistribution", 
                        `title`="By Stage", 
                        `type`="text"),
                    list(
                        `name`="treatmentDistribution", 
                        `title`="By Treatment", 
                        `type`="text"))))
            self$add(jmvcore::Table$new(
                options=options,
                name="survivalStats",
                title="Survival Statistics",
                visible="(survivalVar)",
                rows=0,
                clearWith=list(
                    "survivalVar"),
                columns=list(
                    list(
                        `name`="stage", 
                        `title`="Disease Stage", 
                        `type`="text"),
                    list(
                        `name`="treatment", 
                        `title`="Treatment", 
                        `type`="text"),
                    list(
                        `name`="patients", 
                        `title`="N", 
                        `type`="integer"),
                    list(
                        `name`="events", 
                        `title`="Events", 
                        `type`="integer"),
                    list(
                        `name`="survivalRate", 
                        `title`="Survival Rate", 
                        `type`="number", 
                        `format`="pc"))))}))

alluvialSurvivalBase <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "alluvialSurvivalBase",
    inherit = jmvcore::Analysis,
    public = list(
        initialize = function(options, data=NULL, datasetId="", analysisId="", revision=0) {
            super$initialize(
                package = "ClinicoPath",
                name = "alluvialSurvival",
                version = c(1,0,0),
                options = options,
                results = alluvialSurvivalResults$new(options=options),
                data = data,
                datasetId = datasetId,
                analysisId = analysisId,
                revision = revision,
                pause = NULL,
                completeWhenFilled = FALSE,
                requiresMissings = FALSE,
                weightsSupport = 'auto')
        }))

#' Treatment Pathway Alluvial Plot
#'
#' 
#' @param data .
#' @param timeVar The name of the time variable in the dataset
#' @param stageVar The name of the disease stage variable
#' @param treatmentVar Treatment received at each time point
#' @param patientId Unique patient identifier for tracking
#' @param survivalVar Survival status at each time point
#' @param showRightAxis .
#' @param colorScheme .
#' @param showSurvival .
#' @return A results object containing:
#' \tabular{llllll}{
#'   \code{results$todo} \tab \tab \tab \tab \tab a html \cr
#'   \code{results$mydataview} \tab \tab \tab \tab \tab a preformatted \cr
#'   \code{results$plot} \tab \tab \tab \tab \tab an image \cr
#'   \code{results$summaryTable} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$survivalStats} \tab \tab \tab \tab \tab a table \cr
#' }
#'
#' Tables can be converted to data frames with \code{asDF} or \code{\link{as.data.frame}}. For example:
#'
#' \code{results$summaryTable$asDF}
#'
#' \code{as.data.frame(results$summaryTable)}
#'
#' @export
alluvialSurvival <- function(
    data,
    timeVar,
    stageVar,
    treatmentVar,
    patientId,
    survivalVar,
    showRightAxis = FALSE,
    colorScheme = "clinical",
    showSurvival = FALSE) {

    if ( ! requireNamespace("jmvcore", quietly=TRUE))
        stop("alluvialSurvival requires jmvcore to be installed (restart may be required)")

    if ( ! missing(timeVar)) timeVar <- jmvcore::resolveQuo(jmvcore::enquo(timeVar))
    if ( ! missing(stageVar)) stageVar <- jmvcore::resolveQuo(jmvcore::enquo(stageVar))
    if ( ! missing(treatmentVar)) treatmentVar <- jmvcore::resolveQuo(jmvcore::enquo(treatmentVar))
    if ( ! missing(patientId)) patientId <- jmvcore::resolveQuo(jmvcore::enquo(patientId))
    if ( ! missing(survivalVar)) survivalVar <- jmvcore::resolveQuo(jmvcore::enquo(survivalVar))
    if (missing(data))
        data <- jmvcore::marshalData(
            parent.frame(),
            `if`( ! missing(timeVar), timeVar, NULL),
            `if`( ! missing(stageVar), stageVar, NULL),
            `if`( ! missing(treatmentVar), treatmentVar, NULL),
            `if`( ! missing(patientId), patientId, NULL),
            `if`( ! missing(survivalVar), survivalVar, NULL))

    for (v in stageVar) if (v %in% names(data)) data[[v]] <- as.factor(data[[v]])
    for (v in treatmentVar) if (v %in% names(data)) data[[v]] <- as.factor(data[[v]])
    for (v in survivalVar) if (v %in% names(data)) data[[v]] <- as.factor(data[[v]])

    options <- alluvialSurvivalOptions$new(
        timeVar = timeVar,
        stageVar = stageVar,
        treatmentVar = treatmentVar,
        patientId = patientId,
        survivalVar = survivalVar,
        showRightAxis = showRightAxis,
        colorScheme = colorScheme,
        showSurvival = showSurvival)

    analysis <- alluvialSurvivalClass$new(
        options = options,
        data = data)

    analysis$run()

    analysis$results
}
sbalci/ClinicoPathJamoviModule documentation built on Feb. 25, 2025, 6:34 a.m.