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

benfordOptions <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "benfordOptions",
    inherit = jmvcore::Options,
    public = list(
        initialize = function(
            var = NULL, ...) {

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

            private$..var <- jmvcore::OptionVariable$new(
                "var",
                var,
                suggested=list(
                    "continuous"),
                permitted=list(
                    "numeric"))

            self$.addOption(private$..var)
        }),
    active = list(
        var = function() private$..var$value),
    private = list(
        ..var = NA)
)

benfordResults <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "benfordResults",
    inherit = jmvcore::Group,
    active = list(
        todo = function() private$.items[["todo"]],
        text = function() private$.items[["text"]],
        text2 = function() private$.items[["text2"]],
        plot = function() private$.items[["plot"]]),
    private = list(),
    public=list(
        initialize=function(options) {
            super$initialize(
                options=options,
                name="",
                title="Benford Analysis",
                refs=list(
                    "benford.analysis",
                    "ClinicoPathJamoviModule"))
            self$add(jmvcore::Html$new(
                options=options,
                name="todo",
                title="To Do"))
            self$add(jmvcore::Preformatted$new(
                options=options,
                name="text",
                title="Benford Analysis"))
            self$add(jmvcore::Preformatted$new(
                options=options,
                name="text2",
                title="Suspects"))
            self$add(jmvcore::Image$new(
                options=options,
                name="plot",
                title="Benford Analysis",
                width=600,
                height=450,
                renderFun=".plot",
                requiresData=TRUE))}))

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

#' Benford Analysis
#'
#' 
#' @param data .
#' @param var .
#' @return A results object containing:
#' \tabular{llllll}{
#'   \code{results$todo} \tab \tab \tab \tab \tab a html \cr
#'   \code{results$text} \tab \tab \tab \tab \tab a preformatted \cr
#'   \code{results$text2} \tab \tab \tab \tab \tab a preformatted \cr
#'   \code{results$plot} \tab \tab \tab \tab \tab an image \cr
#' }
#'
#' @export
benford <- function(
    data,
    var) {

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

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


    options <- benfordOptions$new(
        var = var)

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

    analysis$run()

    analysis$results
}

