R/flowchart.h.R

Defines functions flowchart

Documented in flowchart

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

flowchartOptions <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "flowchartOptions",
    inherit = jmvcore::Options,
    public = list(
        initialize = function(
            title = "Study Flow Diagram",
            nodes = NULL,
            counts = NULL,
            direction = "tb",
            style = "simple",
            showCounts = TRUE, ...) {

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

            private$..title <- jmvcore::OptionString$new(
                "title",
                title,
                default="Study Flow Diagram")
            private$..nodes <- jmvcore::OptionVariables$new(
                "nodes",
                nodes,
                suggested=list(
                    "nominal"),
                permitted=list(
                    "factor"))
            private$..counts <- jmvcore::OptionVariables$new(
                "counts",
                counts,
                suggested=list(
                    "continuous"),
                permitted=list(
                    "numeric"))
            private$..direction <- jmvcore::OptionList$new(
                "direction",
                direction,
                options=list(
                    "tb",
                    "lr"),
                default="tb")
            private$..style <- jmvcore::OptionList$new(
                "style",
                style,
                options=list(
                    "simple",
                    "detailed"),
                default="simple")
            private$..showCounts <- jmvcore::OptionBool$new(
                "showCounts",
                showCounts,
                default=TRUE)

            self$.addOption(private$..title)
            self$.addOption(private$..nodes)
            self$.addOption(private$..counts)
            self$.addOption(private$..direction)
            self$.addOption(private$..style)
            self$.addOption(private$..showCounts)
        }),
    active = list(
        title = function() private$..title$value,
        nodes = function() private$..nodes$value,
        counts = function() private$..counts$value,
        direction = function() private$..direction$value,
        style = function() private$..style$value,
        showCounts = function() private$..showCounts$value),
    private = list(
        ..title = NA,
        ..nodes = NA,
        ..counts = NA,
        ..direction = NA,
        ..style = NA,
        ..showCounts = NA)
)

flowchartResults <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "flowchartResults",
    inherit = jmvcore::Group,
    active = list(
        todo = function() private$.items[["todo"]],
        diagram = function() private$.items[["diagram"]],
        diagramCode = function() private$.items[["diagramCode"]]),
    private = list(),
    public=list(
        initialize=function(options) {
            super$initialize(
                options=options,
                name="",
                title="Study Flowchart",
                refs=list(
                    "ClinicoPathJamoviModule"))
            self$add(jmvcore::Html$new(
                options=options,
                name="todo",
                title="To Do"))
            self$add(jmvcore::Image$new(
                options=options,
                name="diagram",
                title="Flowchart Diagram",
                width=800,
                height=600,
                renderFun=".plot",
                requiresData=TRUE,
                clearWith=list(
                    "nodes",
                    "counts",
                    "direction",
                    "style",
                    "showCounts")))
            self$add(jmvcore::Preformatted$new(
                options=options,
                name="diagramCode",
                title="Diagram Code",
                visible=FALSE))}))

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

#' Study Flowchart
#'
#' 
#' @param data The data as a data frame.
#' @param title .
#' @param nodes Variables containing node labels/descriptions
#' @param counts Variables containing counts for each node
#' @param direction .
#' @param style .
#' @param showCounts .
#' @return A results object containing:
#' \tabular{llllll}{
#'   \code{results$todo} \tab \tab \tab \tab \tab a html \cr
#'   \code{results$diagram} \tab \tab \tab \tab \tab an image \cr
#'   \code{results$diagramCode} \tab \tab \tab \tab \tab a preformatted \cr
#' }
#'
#' @export
flowchart <- function(
    data,
    title = "Study Flow Diagram",
    nodes,
    counts,
    direction = "tb",
    style = "simple",
    showCounts = TRUE) {

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

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

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

    options <- flowchartOptions$new(
        title = title,
        nodes = nodes,
        counts = counts,
        direction = direction,
        style = style,
        showCounts = showCounts)

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

    analysis$run()

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