R/pareto.h.R

Defines functions pareto

Documented in pareto

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

paretoOptions <- if (requireNamespace('jmvcore')) R6::R6Class(
    "paretoOptions",
    inherit = jmvcore::Options,
    public = list(
        initialize = function(
            x = NULL,
            counts = NULL,
            angle = 0, ...) {

            super$initialize(
                package='scatr',
                name='pareto',
                requiresData=TRUE,
                ...)

            private$..x <- jmvcore::OptionVariable$new(
                "x",
                x,
                suggested=list(
                    "nominal"),
                permitted=list(
                    "factor"))
            private$..counts <- jmvcore::OptionVariable$new(
                "counts",
                counts,
                default=NULL,
                suggested=list(
                    "continuous"),
                permitted=list(
                    "numeric"))
            private$..angle <- jmvcore::OptionNumber$new(
                "angle",
                angle,
                min=0,
                max=45,
                default=0)

            self$.addOption(private$..x)
            self$.addOption(private$..counts)
            self$.addOption(private$..angle)
        }),
    active = list(
        x = function() private$..x$value,
        counts = function() private$..counts$value,
        angle = function() private$..angle$value),
    private = list(
        ..x = NA,
        ..counts = NA,
        ..angle = NA)
)

paretoResults <- if (requireNamespace('jmvcore')) R6::R6Class(
    inherit = jmvcore::Group,
    active = list(
        pareto = function() private$.items[["pareto"]]),
    private = list(),
    public=list(
        initialize=function(options) {
            super$initialize(
                options=options,
                name="",
                title="Pareto Chart")
            self$add(jmvcore::Image$new(
                options=options,
                name="pareto",
                title="",
                renderFun=".pareto",
                clearWith=list(
                    "x",
                    "counts",
                    "angle")))}))

paretoBase <- if (requireNamespace('jmvcore')) R6::R6Class(
    "paretoBase",
    inherit = jmvcore::Analysis,
    public = list(
        initialize = function(options, data=NULL, datasetId="", analysisId="", revision=0) {
            super$initialize(
                package = 'scatr',
                name = 'pareto',
                version = c(1,1,0),
                options = options,
                results = paretoResults$new(options=options),
                data = data,
                datasetId = datasetId,
                analysisId = analysisId,
                revision = revision,
                pause = NULL,
                completeWhenFilled = FALSE)
        }))

#' Pareto Chart
#'
#' Function for making pareto charts.
#'
#' @examples
#' set.seed(1337)
#'
#' X <- sample(c('A','B','C','D','E','F'), 100, replace=TRUE)
#' dat <- data.frame(X = X)
#'
#' pareto(dat, x = 'X')
#'
#' @param data the data as a data frame
#' @param x a string naming the variable from \code{data} that contains the
#'   values used for the chart
#' @param counts a string naming the variable from \code{data} that contains
#'   the  counts for the values (optional)
#' @param angle a number from 0 to 45 defining the angle of the x-axis labels,
#'   where 0 degrees represents completely horizontal labels.
#' @return A results object containing:
#' \tabular{llllll}{
#'   \code{results$pareto} \tab \tab \tab \tab \tab a Pareto chart \cr
#' }
#'
#' @export
pareto <- function(
    data,
    x,
    counts = NULL,
    angle = 0) {

    if ( ! requireNamespace('jmvcore'))
        stop('pareto requires jmvcore to be installed (restart may be required)')

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

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

    options <- paretoOptions$new(
        x = x,
        counts = counts,
        angle = angle)

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

    analysis$run()

    analysis$results
}
raviselker/scatr documentation built on Nov. 12, 2019, 4:51 a.m.