R/PreprocessorSum.r

#' PreprocessorSum class.
#'
#' It can be used to sum variables in order to create a new variable.
#' @export PreprocessorSum
#' @exportClass PreprocessorSum
PreprocessorSum <- R6::R6Class(
    classname = "PreprocessorSum",
    inherit = Preprocessor,

    ## Properties
    private = list(

        ## Processor function
        .process = function(inputValues) {
            len <- length(rhaskell::head(inputValues)) # cannot be empty due to check in calling (=parent) class
            return(rhaskell::foldl(function(acc, x) acc + x, base::vector("numeric", len), inputValues))
        },
        .getDefaultDesc = function() {
            return(paste0("Sum(", paste(self$inputNames, collapse = ", "), ")"))
        }
    ),

    ## Methods
    public = list(
        initialize = function(outputName, inputNames, deleteInputVars = FALSE, nodeDesc = NULL) {
            self$inputNames <- inputNames
            if (is.null(nodeDesc)) nodeDesc <- paste0(outputName, " <- ", private$.getDefaultDesc())
            super$initialize(outputName, inputNames, deleteInputVars, nodeDesc)
        }
    )

)
schnecki/ranalyse documentation built on Dec. 1, 2022, 8:57 p.m.