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

oneSurvivalOptions <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "oneSurvivalOptions",
    inherit = jmvcore::Options,
    public = list(
        initialize = function(
            times = NULL,
            status = NULL,
            ciyn = FALSE,
            timeunits = "None", ...) {

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

            private$..times <- jmvcore::OptionVariable$new(
                "times",
                times,
                suggested=list(
                    "continuous"))
            private$..status <- jmvcore::OptionVariable$new(
                "status",
                status,
                suggested=list(
                    "continuous"))
            private$..ciyn <- jmvcore::OptionBool$new(
                "ciyn",
                ciyn,
                default=FALSE)
            private$..timeunits <- jmvcore::OptionList$new(
                "timeunits",
                timeunits,
                options=list(
                    "None",
                    "Days",
                    "Weeks",
                    "Months",
                    "Years"),
                default="None")

            self$.addOption(private$..times)
            self$.addOption(private$..status)
            self$.addOption(private$..ciyn)
            self$.addOption(private$..timeunits)
        }),
    active = list(
        times = function() private$..times$value,
        status = function() private$..status$value,
        ciyn = function() private$..ciyn$value,
        timeunits = function() private$..timeunits$value),
    private = list(
        ..times = NA,
        ..status = NA,
        ..ciyn = NA,
        ..timeunits = NA)
)

oneSurvivalResults <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "oneSurvivalResults",
    inherit = jmvcore::Group,
    active = list(
        text = function() private$.items[["text"]],
        onesurvTable1 = function() private$.items[["onesurvTable1"]],
        onesurvPlot1 = function() private$.items[["onesurvPlot1"]]),
    private = list(),
    public=list(
        initialize=function(options) {
            super$initialize(
                options=options,
                name="",
                title="One Survival Outcome",
                refs=list(
                    "ClinicoPathJamoviModule"))
            self$add(jmvcore::Preformatted$new(
                options=options,
                name="text",
                title="One Survival Outcome"))
            self$add(jmvcore::Table$new(
                options=options,
                name="onesurvTable1",
                title="Median Estimates",
                clearWith=list(
                    "times",
                    "status"),
                rows=1,
                columns=list(
                    list(
                        `name`="n", 
                        `title`="Number of Subjects", 
                        `type`="integer"),
                    list(
                        `name`="nevents", 
                        `title`="Number of Events", 
                        `type`="integer"),
                    list(
                        `name`="median", 
                        `title`="Median", 
                        `type`="number"),
                    list(
                        `name`="cilb", 
                        `title`="Lower", 
                        `type`="number"),
                    list(
                        `name`="ciub", 
                        `title`="Upper", 
                        `type`="number"))))
            self$add(jmvcore::Image$new(
                options=options,
                name="onesurvPlot1",
                title="Kaplan-Meier estimator of the survivor function",
                width=600,
                height=500,
                renderFun=".plot"))}))

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

#' One Survival Outcome
#'
#' 
#' @param data .
#' @param times .
#' @param status .
#' @param ciyn .
#' @param timeunits .
#' @return A results object containing:
#' \tabular{llllll}{
#'   \code{results$text} \tab \tab \tab \tab \tab a preformatted \cr
#'   \code{results$onesurvTable1} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$onesurvPlot1} \tab \tab \tab \tab \tab an image \cr
#' }
#'
#' Tables can be converted to data frames with \code{asDF} or \code{\link{as.data.frame}}. For example:
#'
#' \code{results$onesurvTable1$asDF}
#'
#' \code{as.data.frame(results$onesurvTable1)}
#'
#' @export
oneSurvival <- function(
    data,
    times,
    status,
    ciyn = FALSE,
    timeunits = "None") {

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

    if ( ! missing(times)) times <- jmvcore::resolveQuo(jmvcore::enquo(times))
    if ( ! missing(status)) status <- jmvcore::resolveQuo(jmvcore::enquo(status))
    if (missing(data))
        data <- jmvcore::marshalData(
            parent.frame(),
            `if`( ! missing(times), times, NULL),
            `if`( ! missing(status), status, NULL))


    options <- oneSurvivalOptions$new(
        times = times,
        status = status,
        ciyn = ciyn,
        timeunits = timeunits)

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

    analysis$run()

    analysis$results
}

