R/riverplot.h.R

Defines functions riverplot

Documented in riverplot

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

riverplotOptions <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "riverplotOptions",
    inherit = jmvcore::Options,
    public = list(
        initialize = function(
            id = NULL,
            time = NULL,
            strata = NULL,
            weight = NULL,
            plotType = "alluvial",
            fillType = "first",
            sortStreams = TRUE,
            labelNodes = TRUE,
            curveType = "cardinal",
            showCounts = FALSE,
            showLegend = TRUE,
            mytitle = "",
            xtitle = "",
            ytitle = "",
            originaltheme = FALSE, ...) {

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

            private$..id <- jmvcore::OptionVariable$new(
                "id",
                id,
                suggested=list(
                    "nominal",
                    "ordinal"),
                default=NULL)
            private$..time <- jmvcore::OptionVariable$new(
                "time",
                time,
                suggested=list(
                    "ordinal",
                    "nominal"),
                permitted=list(
                    "factor"))
            private$..strata <- jmvcore::OptionVariables$new(
                "strata",
                strata,
                suggested=list(
                    "ordinal",
                    "nominal"),
                permitted=list(
                    "factor"))
            private$..weight <- jmvcore::OptionVariable$new(
                "weight",
                weight,
                suggested=list(
                    "continuous"),
                permitted=list(
                    "numeric"),
                default=NULL)
            private$..plotType <- jmvcore::OptionList$new(
                "plotType",
                plotType,
                options=list(
                    "alluvial",
                    "sankey",
                    "stream"),
                default="alluvial")
            private$..fillType <- jmvcore::OptionList$new(
                "fillType",
                fillType,
                options=list(
                    "first",
                    "last",
                    "frequency"),
                default="first")
            private$..sortStreams <- jmvcore::OptionBool$new(
                "sortStreams",
                sortStreams,
                default=TRUE)
            private$..labelNodes <- jmvcore::OptionBool$new(
                "labelNodes",
                labelNodes,
                default=TRUE)
            private$..curveType <- jmvcore::OptionList$new(
                "curveType",
                curveType,
                options=list(
                    "linear",
                    "cardinal",
                    "basis",
                    "step"),
                default="cardinal")
            private$..showCounts <- jmvcore::OptionBool$new(
                "showCounts",
                showCounts,
                default=FALSE)
            private$..showLegend <- jmvcore::OptionBool$new(
                "showLegend",
                showLegend,
                default=TRUE)
            private$..mytitle <- jmvcore::OptionString$new(
                "mytitle",
                mytitle,
                default="")
            private$..xtitle <- jmvcore::OptionString$new(
                "xtitle",
                xtitle,
                default="")
            private$..ytitle <- jmvcore::OptionString$new(
                "ytitle",
                ytitle,
                default="")
            private$..originaltheme <- jmvcore::OptionBool$new(
                "originaltheme",
                originaltheme,
                default=FALSE)

            self$.addOption(private$..id)
            self$.addOption(private$..time)
            self$.addOption(private$..strata)
            self$.addOption(private$..weight)
            self$.addOption(private$..plotType)
            self$.addOption(private$..fillType)
            self$.addOption(private$..sortStreams)
            self$.addOption(private$..labelNodes)
            self$.addOption(private$..curveType)
            self$.addOption(private$..showCounts)
            self$.addOption(private$..showLegend)
            self$.addOption(private$..mytitle)
            self$.addOption(private$..xtitle)
            self$.addOption(private$..ytitle)
            self$.addOption(private$..originaltheme)
        }),
    active = list(
        id = function() private$..id$value,
        time = function() private$..time$value,
        strata = function() private$..strata$value,
        weight = function() private$..weight$value,
        plotType = function() private$..plotType$value,
        fillType = function() private$..fillType$value,
        sortStreams = function() private$..sortStreams$value,
        labelNodes = function() private$..labelNodes$value,
        curveType = function() private$..curveType$value,
        showCounts = function() private$..showCounts$value,
        showLegend = function() private$..showLegend$value,
        mytitle = function() private$..mytitle$value,
        xtitle = function() private$..xtitle$value,
        ytitle = function() private$..ytitle$value,
        originaltheme = function() private$..originaltheme$value),
    private = list(
        ..id = NA,
        ..time = NA,
        ..strata = NA,
        ..weight = NA,
        ..plotType = NA,
        ..fillType = NA,
        ..sortStreams = NA,
        ..labelNodes = NA,
        ..curveType = NA,
        ..showCounts = NA,
        ..showLegend = NA,
        ..mytitle = NA,
        ..xtitle = NA,
        ..ytitle = NA,
        ..originaltheme = NA)
)

riverplotResults <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "riverplotResults",
    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="River Plots",
                refs=list(
                    "ggplot2",
                    "ggalluvial",
                    "easyalluvial",
                    "ClinicoPathJamoviModule"),
                clearWith=list(
                    "id",
                    "time",
                    "strata",
                    "weight",
                    "plotType",
                    "fillType",
                    "sortStreams",
                    "labelNodes",
                    "curveType",
                    "showCounts",
                    "showLegend",
                    "originaltheme"))
            self$add(jmvcore::Html$new(
                options=options,
                name="todo",
                title="To Do"))
            self$add(jmvcore::Image$new(
                options=options,
                name="plot",
                title="River Plot",
                width=800,
                height=600,
                renderFun=".plot",
                requiresData=TRUE))}))

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

#' River Plots
#'
#' 'Function for generating river plots (alluvial diagrams) to visualize
#' flows and transitions over time or between categories.'
#' 
#'
#' @examples
#' \donttest{
#' # example will be added
#'}
#' @param data The data as a data frame.
#' @param id Optional identifier for individual entities in the data.
#' @param time Variable representing time points or sequential stages.
#' @param strata Variables containing the categories that change over time.
#' @param weight Optional numerical variable to determine stream width.
#' @param plotType Type of river plot to generate.
#' @param fillType Determines how colors are assigned to flows.
#' @param sortStreams Sort alluvial streams by frequency.
#' @param labelNodes Add labels to nodes.
#' @param curveType Type of curve to use for stream paths.
#' @param showCounts Display counts on the diagram.
#' @param showLegend Display color legend.
#' @param mytitle Title for the plot.
#' @param xtitle Label for the x-axis.
#' @param ytitle Label for the y-axis.
#' @param originaltheme Use the ggStatsPlot theme instead of the default
#'   theme.
#' @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
riverplot <- function(
    data,
    id = NULL,
    time,
    strata,
    weight = NULL,
    plotType = "alluvial",
    fillType = "first",
    sortStreams = TRUE,
    labelNodes = TRUE,
    curveType = "cardinal",
    showCounts = FALSE,
    showLegend = TRUE,
    mytitle = "",
    xtitle = "",
    ytitle = "",
    originaltheme = FALSE) {

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

    if ( ! missing(id)) id <- jmvcore::resolveQuo(jmvcore::enquo(id))
    if ( ! missing(time)) time <- jmvcore::resolveQuo(jmvcore::enquo(time))
    if ( ! missing(strata)) strata <- jmvcore::resolveQuo(jmvcore::enquo(strata))
    if ( ! missing(weight)) weight <- jmvcore::resolveQuo(jmvcore::enquo(weight))
    if (missing(data))
        data <- jmvcore::marshalData(
            parent.frame(),
            `if`( ! missing(id), id, NULL),
            `if`( ! missing(time), time, NULL),
            `if`( ! missing(strata), strata, NULL),
            `if`( ! missing(weight), weight, NULL))

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

    options <- riverplotOptions$new(
        id = id,
        time = time,
        strata = strata,
        weight = weight,
        plotType = plotType,
        fillType = fillType,
        sortStreams = sortStreams,
        labelNodes = labelNodes,
        curveType = curveType,
        showCounts = showCounts,
        showLegend = showLegend,
        mytitle = mytitle,
        xtitle = xtitle,
        ytitle = ytitle,
        originaltheme = originaltheme)

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

    analysis$run()

    analysis$results
}
sbalci/ClinicoPathJamoviModule documentation built on June 13, 2025, 9:34 a.m.