R/waffle.h.R

Defines functions waffle

Documented in waffle

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

waffleOptions <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "waffleOptions",
    inherit = jmvcore::Options,
    public = list(
        initialize = function(
            counts = NULL,
            groups = NULL,
            facet = NULL,
            rows = 5,
            flip = FALSE,
            color_palette = "default",
            show_legend = TRUE,
            mytitle = "",
            legendtitle = "", ...) {

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

            private$..counts <- jmvcore::OptionVariable$new(
                "counts",
                counts,
                suggested=list(
                    "continuous"),
                permitted=list(
                    "numeric"))
            private$..groups <- jmvcore::OptionVariable$new(
                "groups",
                groups,
                suggested=list(
                    "nominal",
                    "ordinal"),
                permitted=list(
                    "factor"))
            private$..facet <- jmvcore::OptionVariable$new(
                "facet",
                facet,
                suggested=list(
                    "nominal",
                    "ordinal"),
                permitted=list(
                    "factor"))
            private$..rows <- jmvcore::OptionInteger$new(
                "rows",
                rows,
                default=5,
                min=1)
            private$..flip <- jmvcore::OptionBool$new(
                "flip",
                flip,
                default=FALSE)
            private$..color_palette <- jmvcore::OptionList$new(
                "color_palette",
                color_palette,
                options=list(
                    "default",
                    "colorblind",
                    "professional",
                    "presentation",
                    "journal",
                    "pastel",
                    "dark"),
                default="default")
            private$..show_legend <- jmvcore::OptionBool$new(
                "show_legend",
                show_legend,
                default=TRUE)
            private$..mytitle <- jmvcore::OptionString$new(
                "mytitle",
                mytitle,
                default="")
            private$..legendtitle <- jmvcore::OptionString$new(
                "legendtitle",
                legendtitle,
                default="")

            self$.addOption(private$..counts)
            self$.addOption(private$..groups)
            self$.addOption(private$..facet)
            self$.addOption(private$..rows)
            self$.addOption(private$..flip)
            self$.addOption(private$..color_palette)
            self$.addOption(private$..show_legend)
            self$.addOption(private$..mytitle)
            self$.addOption(private$..legendtitle)
        }),
    active = list(
        counts = function() private$..counts$value,
        groups = function() private$..groups$value,
        facet = function() private$..facet$value,
        rows = function() private$..rows$value,
        flip = function() private$..flip$value,
        color_palette = function() private$..color_palette$value,
        show_legend = function() private$..show_legend$value,
        mytitle = function() private$..mytitle$value,
        legendtitle = function() private$..legendtitle$value),
    private = list(
        ..counts = NA,
        ..groups = NA,
        ..facet = NA,
        ..rows = NA,
        ..flip = NA,
        ..color_palette = NA,
        ..show_legend = NA,
        ..mytitle = NA,
        ..legendtitle = NA)
)

waffleResults <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "waffleResults",
    inherit = jmvcore::Group,
    active = list(
        todo = function() private$.items[["todo"]],
        plot = function() private$.items[["plot"]]),
    private = list(),
    public=list(
        initialize=function(options) {
            super$initialize(
                options=options,
                name="",
                title="Waffle Charts",
                refs=list(
                    "ggplot2",
                    "waffle",
                    "ClinicoPathJamoviModule"),
                clearWith=list(
                    "counts",
                    "groups",
                    "facet",
                    "rows",
                    "flip",
                    "color_palette",
                    "show_legend",
                    "mytitle",
                    "legendtitle"))
            self$add(jmvcore::Html$new(
                options=options,
                name="todo",
                title="To Do"))
            self$add(jmvcore::Image$new(
                options=options,
                name="plot",
                title="Waffle Chart",
                width=600,
                height=450,
                renderFun=".plot",
                requiresData=TRUE))}))

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

#' Waffle Charts
#'
#' 'Creates waffle charts to visualize distributions and proportions'
#' 
#' @param data The data as a data frame.
#' @param counts Optional numeric values to be represented in the waffle
#'   chart. If not provided, will use number of cases.
#' @param groups The grouping variable for the waffle squares
#' @param facet Optional variable to create faceted waffle charts
#' @param rows Number of rows in the waffle chart
#' @param flip Whether to flip the orientation of the waffle chart
#' @param color_palette Color scheme for the waffle squares
#' @param show_legend Whether to display the legend
#' @param mytitle Custom title for the plot
#' @param legendtitle Custom title for the legend
#' @return A results object containing:
#' \tabular{llllll}{
#'   \code{results$todo} \tab \tab \tab \tab \tab a html \cr
#'   \code{results$plot} \tab \tab \tab \tab \tab an image \cr
#' }
#'
#' @export
waffle <- function(
    data,
    counts,
    groups,
    facet,
    rows = 5,
    flip = FALSE,
    color_palette = "default",
    show_legend = TRUE,
    mytitle = "",
    legendtitle = "") {

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

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

    for (v in groups) if (v %in% names(data)) data[[v]] <- as.factor(data[[v]])
    for (v in facet) if (v %in% names(data)) data[[v]] <- as.factor(data[[v]])

    options <- waffleOptions$new(
        counts = counts,
        groups = groups,
        facet = facet,
        rows = rows,
        flip = flip,
        color_palette = color_palette,
        show_legend = show_legend,
        mytitle = mytitle,
        legendtitle = legendtitle)

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

    analysis$run()

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