R/jjstreamgraph.h.R

Defines functions jjstreamgraph

Documented in jjstreamgraph

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

jjstreamgraphOptions <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "jjstreamgraphOptions",
    inherit = jmvcore::Options,
    public = list(
        initialize = function(
            timeVar = NULL,
            valueVar = NULL,
            groupVar = NULL,
            offset = "silhouette",
            interpolate = "cardinal",
            palette = "Blues",
            width = "800px",
            height = "500px", ...) {

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

            private$..timeVar <- jmvcore::OptionVariable$new(
                "timeVar",
                timeVar,
                suggested=list(
                    "continuous"),
                permitted=list(
                    "numeric"))
            private$..valueVar <- jmvcore::OptionVariable$new(
                "valueVar",
                valueVar,
                suggested=list(
                    "continuous"),
                permitted=list(
                    "numeric"))
            private$..groupVar <- jmvcore::OptionVariable$new(
                "groupVar",
                groupVar,
                suggested=list(
                    "nominal",
                    "ordinal"),
                permitted=list(
                    "factor"))
            private$..offset <- jmvcore::OptionList$new(
                "offset",
                offset,
                options=list(
                    "silhouette",
                    "zero",
                    "expand"),
                default="silhouette")
            private$..interpolate <- jmvcore::OptionList$new(
                "interpolate",
                interpolate,
                options=list(
                    "cardinal",
                    "linear",
                    "step"),
                default="cardinal")
            private$..palette <- jmvcore::OptionList$new(
                "palette",
                palette,
                options=list(
                    "Blues",
                    "Greens",
                    "Reds",
                    "Pastel1",
                    "Set1"),
                default="Blues")
            private$..width <- jmvcore::OptionString$new(
                "width",
                width,
                default="800px")
            private$..height <- jmvcore::OptionString$new(
                "height",
                height,
                default="500px")

            self$.addOption(private$..timeVar)
            self$.addOption(private$..valueVar)
            self$.addOption(private$..groupVar)
            self$.addOption(private$..offset)
            self$.addOption(private$..interpolate)
            self$.addOption(private$..palette)
            self$.addOption(private$..width)
            self$.addOption(private$..height)
        }),
    active = list(
        timeVar = function() private$..timeVar$value,
        valueVar = function() private$..valueVar$value,
        groupVar = function() private$..groupVar$value,
        offset = function() private$..offset$value,
        interpolate = function() private$..interpolate$value,
        palette = function() private$..palette$value,
        width = function() private$..width$value,
        height = function() private$..height$value),
    private = list(
        ..timeVar = NA,
        ..valueVar = NA,
        ..groupVar = NA,
        ..offset = NA,
        ..interpolate = NA,
        ..palette = NA,
        ..width = NA,
        ..height = NA)
)

jjstreamgraphResults <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "jjstreamgraphResults",
    inherit = jmvcore::Group,
    active = list(
        StreamGraph = function() private$.items[["StreamGraph"]]),
    private = list(),
    public=list(
        initialize=function(options) {
            super$initialize(
                options=options,
                name="",
                title="StreamGraphs",
                refs=list(
                    "streamgraph"))
            self$add(jmvcore::Html$new(
                options=options,
                name="StreamGraph",
                title="StreamGraph"))}))

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

#' StreamGraphs
#'
#' 'Create interactive StreamGraphs using R streamgraph package.'
#' 
#'
#' @examples
#' \donttest{
#' # Example will be added
#'}
#' @param data The data as a data frame.
#' @param timeVar Variable representing time points (x-axis)
#' @param valueVar Variable containing values to be plotted (y-axis)
#' @param groupVar Categorical variable for grouping data into streams
#' @param offset Type of offset to use for the streamgraph
#' @param interpolate Type of interpolation between points
#' @param palette ColorBrewer palette to use for streams
#' @param width Width of the plot
#' @param height Height of the plot
#' @return A results object containing:
#' \tabular{llllll}{
#'   \code{results$StreamGraph} \tab \tab \tab \tab \tab a html \cr
#' }
#'
#' @export
jjstreamgraph <- function(
    data,
    timeVar,
    valueVar,
    groupVar,
    offset = "silhouette",
    interpolate = "cardinal",
    palette = "Blues",
    width = "800px",
    height = "500px") {

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

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

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

    options <- jjstreamgraphOptions$new(
        timeVar = timeVar,
        valueVar = valueVar,
        groupVar = groupVar,
        offset = offset,
        interpolate = interpolate,
        palette = palette,
        width = width,
        height = height)

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

    analysis$run()

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