R/TableCleaner-R6.R

#' @title Clean a Table
#'
#' @description Given \code{table} and a \code{path} to its profile generated by
#'   \code{\link{generate_table_profile}}, when \code{TableCleaner$new(table,
#'   path)} is called, then the user can choose to apply some built-in data
#'   preprocessing operations on \code{table}.
#'
#' @return (`TableCleaner`) An object with the table profile.
#'
#' @export
#'
#' @examples
#' \dontrun{
#' mtcars_cleaner <- TableCleaner$new(table = mtcars, path = file.path("data", "db_schema"))
#' mtcars_tidy <- mtcars_cleaner$drop_rows_with_na()$table
#' }
#'
TableCleaner <- R6::R6Class(
    classname = "TableCleaner",
    public = list(
        ## Public Methods
        initialize = function(table, path){

            stopifnot(table %>% missing() %>% isFALSE(),
                      path %>% missing() %>% isFALSE())
            .assert_is_non_empty_data.frame(table)
            .assert_is_a_non_missing_nor_empty_string(path)

            private$.path <- path
            private$.table <- table
            private$.table_name <- deparse(substitute(table))
            private$.profile_path <- file.path(private$.path, paste0(private$.table_name ,".yml"))

            if(file.exists(private$.profile_path)){
                private$.profile <- yaml::read_yaml(private$.profile_path)
            } else {
                private$.profile <- self$generate_table_profile(table)
                yaml::write_yaml(private$.profile, private$.profile_path)
            }

        }, # end initialize
        generate_table_profile = function(table) generate_table_profile(table),
        drop_rows_with_na = function() private$.table <- drop_rows_with_na(private$.table, private$.table_profile)
    ),
    private = list(
        ## Private Variables
        .path = character(),
        .profile = list(),
        .profile_path = character(),
        .table = data.frame(),
        .table_name = character()
        ## Private Methods
    ),
    active = list(table = function() return(private$.table))
)# end R6
tidylab/tidylab.dqa documentation built on June 21, 2019, 7 p.m.