R/tableone.h.R

Defines functions tableone

Documented in tableone

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

tableoneOptions <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "tableoneOptions",
    inherit = jmvcore::Options,
    public = list(
        initialize = function(
            vars = NULL,
            sty = "t1",
            excl = FALSE,
            pivot_format = "clinical",
            include_statistics = TRUE,
            group_comparisons = FALSE, ...) {

            super$initialize(
                package="ClinicoPathDescriptives",
                name="tableone",
                requiresData=TRUE,
                ...)

            private$..vars <- jmvcore::OptionVariables$new(
                "vars",
                vars)
            private$..sty <- jmvcore::OptionList$new(
                "sty",
                sty,
                options=list(
                    "t1",
                    "t2",
                    "t3",
                    "t4",
                    "t5"),
                default="t1")
            private$..excl <- jmvcore::OptionBool$new(
                "excl",
                excl,
                default=FALSE)
            private$..pivot_format <- jmvcore::OptionList$new(
                "pivot_format",
                pivot_format,
                options=list(
                    "clinical",
                    "publication",
                    "detailed"),
                default="clinical")
            private$..include_statistics <- jmvcore::OptionBool$new(
                "include_statistics",
                include_statistics,
                default=TRUE)
            private$..group_comparisons <- jmvcore::OptionBool$new(
                "group_comparisons",
                group_comparisons,
                default=FALSE)

            self$.addOption(private$..vars)
            self$.addOption(private$..sty)
            self$.addOption(private$..excl)
            self$.addOption(private$..pivot_format)
            self$.addOption(private$..include_statistics)
            self$.addOption(private$..group_comparisons)
        }),
    active = list(
        vars = function() private$..vars$value,
        sty = function() private$..sty$value,
        excl = function() private$..excl$value,
        pivot_format = function() private$..pivot_format$value,
        include_statistics = function() private$..include_statistics$value,
        group_comparisons = function() private$..group_comparisons$value),
    private = list(
        ..vars = NA,
        ..sty = NA,
        ..excl = NA,
        ..pivot_format = NA,
        ..include_statistics = NA,
        ..group_comparisons = NA)
)

tableoneResults <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "tableoneResults",
    inherit = jmvcore::Group,
    active = list(
        todo = function() private$.items[["todo"]],
        tablestyle1 = function() private$.items[["tablestyle1"]],
        tablestyle2 = function() private$.items[["tablestyle2"]],
        tablestyle3 = function() private$.items[["tablestyle3"]],
        tablestyle4 = function() private$.items[["tablestyle4"]],
        tablestyle5 = function() private$.items[["tablestyle5"]]),
    private = list(),
    public=list(
        initialize=function(options) {
            super$initialize(
                options=options,
                name="",
                title="Table One",
                refs=list(
                    "ClinicoPathJamoviModule",
                    "whoisinthisstudy"))
            self$add(jmvcore::Html$new(
                options=options,
                name="todo",
                title="Instructions"))
            self$add(jmvcore::Preformatted$new(
                options=options,
                name="tablestyle1",
                title="Table One Output (tableone)",
                clearWith=list(
                    "vars",
                    "excl"),
                visible="(sty:t1)",
                refs="tableone"))
            self$add(jmvcore::Html$new(
                options=options,
                name="tablestyle2",
                title="Summary Table (gtsummary)",
                clearWith=list(
                    "vars",
                    "excl"),
                visible="(sty:t2)",
                refs="gtsummary"))
            self$add(jmvcore::Html$new(
                options=options,
                name="tablestyle3",
                title="Descriptive Table (arsenal)",
                clearWith=list(
                    "vars",
                    "excl"),
                visible="(sty:t3)",
                refs="arsenal"))
            self$add(jmvcore::Html$new(
                options=options,
                name="tablestyle4",
                title="Frequency Tables (janitor)",
                clearWith=list(
                    "vars",
                    "excl"),
                visible="(sty:t4)",
                refs="janitor"))
            self$add(jmvcore::Html$new(
                options=options,
                name="tablestyle5",
                title="Enhanced Table One (pivottabler)",
                clearWith=list(
                    "vars",
                    "excl",
                    "pivot_format",
                    "include_statistics",
                    "group_comparisons"),
                visible="(sty:t5)"))}))

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

#' Table One
#'
#' This function generates a "Table One", a descriptive summary table 
#' frequently used in  clinicopathological research manuscripts. It supports 
#' multiple output styles for flexible formatting.
#' 
#'
#' @examples
#' \donttest{
#' # Example usage:
#' # data('histopathology')
#' # dat <- as.data.frame(histopathology)
#' # ClinicoPath::tableone(
#' #   data = dat,
#' #   vars = vars(Sex, PreinvasiveComponent, LVI, PNI, Grade, Age),
#' #   sty = "t3",
#' #   excl = TRUE)
#'}
#' @param data The input data as a data frame.
#' @param vars A set of variable names from \code{data} to include in the
#'   Table One. Supports numeric, ordinal, and categorical variables.
#' @param sty Specify the output style for the table. Each option uses a
#'   different package for formatting.
#' @param excl Boolean option to exclude missing values (NA) from the
#'   analysis. Note: Exclusion may remove entire cases.
#' @param pivot_format Formatting style for pivottabler enhanced tables.
#' @param include_statistics Include advanced statistical summaries in pivot
#'   format.
#' @param group_comparisons Enable group comparison features in pivot table.
#' @return A results object containing:
#' \tabular{llllll}{
#'   \code{results$todo} \tab \tab \tab \tab \tab a html \cr
#'   \code{results$tablestyle1} \tab \tab \tab \tab \tab a preformatted \cr
#'   \code{results$tablestyle2} \tab \tab \tab \tab \tab a html \cr
#'   \code{results$tablestyle3} \tab \tab \tab \tab \tab a html \cr
#'   \code{results$tablestyle4} \tab \tab \tab \tab \tab a html \cr
#'   \code{results$tablestyle5} \tab \tab \tab \tab \tab a html \cr
#' }
#'
#' @export
tableone <- function(
    data,
    vars,
    sty = "t1",
    excl = FALSE,
    pivot_format = "clinical",
    include_statistics = TRUE,
    group_comparisons = FALSE) {

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

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


    options <- tableoneOptions$new(
        vars = vars,
        sty = sty,
        excl = excl,
        pivot_format = pivot_format,
        include_statistics = include_statistics,
        group_comparisons = group_comparisons)

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

    analysis$run()

    analysis$results
}
sbalci/ClinicoPathDescriptives documentation built on July 4, 2025, 5:25 p.m.