R/ubiqueR.R

#' ubiqueR class
#'
#' Quantitative and Machine Learning Methods for Portfolio and Risk Analysis
#'
#' A package for quantitative portfolio and risk management for practitioners.
#' It provides custom functions or a wrapper to packages 'stats','PerformanceAnalytics','PortolioAnalytics' and much more.
#' 
#' Based on \code{R6Class}, it's possible to chain methods 
#' 
#' @name ubiqueR
#' @usage
#' \preformatted{
#' Initialize the constructor
#'
#' data("sample_matrix")
#' p <- ubiqueR::ubiqueR$new(.data = sample_matrix)
#' 
#' p$class() 
#' # "matrix"
#' 
#' p$randn(5,4)$dim()
#' # 5 4
#' 
#' }
#' @section Public Fields:
#' \describe{
#'   \item{\code{.data}}{Stored input data}
#'  }
#' @section Methods:
#' \describe{
#'   \item{\code{set(.data)}}{Set input data}
#'   \item{\code{get()}}{Get stored data}
#'   \item{\code{class()}}{Object class}
#'   \item{\code{dim()}}{Data dimension MxN}
#'   \item{\code{randn(...)}}{Random generation for the normal distribution \link[ubiqueR]{randn}}
#'   \item{\code{unifrnd(...)}}{Random generation for the uniform distribution \link[ubiqueR]{unifrnd}}
#'  }
NULL


# main class 
#' @export
ubiqueR <- R6::R6Class("ubiqueR",
                       public = list(
                         # fields to store data internally
                         .data = NULL,
                         # initialize
                         initialize = function(.data = NULL) {
                           self$.data <- .data
                         },
                         # set internal field .data with a new object
                         set = function(.data) {
                           self$.data <- .data
                         },
                         # get .data
                         get = function() {
                           self$.data
                         },
                         # print output
                         print = function() {
                           print(self$.data)
                         },
                         # object class
                         class = function() {
                           base::class(self$.data)
                         },
                         # finalize
                         finalize = function() {
                           #
                         },
                         # object dimension
                         dim = function() {
                           base::dim(self$.data)
                         }
                       )
)

#' NA omit from matrix
#' 
#' @name na_omit
ubiqueR$set("public","na_omit",function() {
  self <- self$clone()
  self$.data <- stats::na.omit(self$.data)
  self
})

#' TRUE if object is empty
#' 
#' @name is_empty
ubiqueR$set("public","is_empty",function() {
  .data <- self$.data
  if (class(.data) == "data.frame") {
    return ((!nrow(.data)) || (nrow(.data) && !length(.data)))
  } else if (class(.data) == "NULL") {
    return (TRUE)
  } else if (class(.data) == "character") {
    return (length(.data) == 0 || (length(.data) == 1 && nchar(.data) == 0))
  } else if (class(.data) == "list") {
    return (length(.data) == 0)
  } else if (class(.data) == "integer") {
    return (length(.data) == 0)
  }
})

# attach methods from R files
fun_list <- list.files("./R")
for (i in 1:length(fun_list)) {
  if (!base::isTRUE(fun_list[i] %in% c("ubiqueR.R"))) {
    fname <- sub(pattern = ".R",replacement = "",x = fun_list[i])
    ubiqueR$set("public",fname,source(paste0("./R/",fun_list[i]),local = T)$value)
  }
}
maxto/ubiqueR documentation built on Nov. 22, 2017, 12:04 a.m.