diff --git a/.Rbuildignore b/.Rbuildignore index e543a5f..c6ce9f7 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -1,3 +1,5 @@ ^LICENSE\.md$ ^README\.Rmd$ ^\.github$ +^.*\.Rproj$ +^\.Rproj\.user$ diff --git a/DESCRIPTION b/DESCRIPTION index c818c9f..97651f9 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -10,4 +10,8 @@ Encoding: UTF-8 Roxygen: list(markdown = TRUE) RoxygenNote: 7.2.3 Imports: - magrittr + data.table, + magrittr, + R6, + tibble, + zoo diff --git a/NAMESPACE b/NAMESPACE index ea39623..f777c98 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,4 +1,5 @@ # Generated by roxygen2: do not edit by hand export("%>%") +export(DataSheet) importFrom(magrittr,"%>%") diff --git a/R/data_book.R b/R/data_book.R new file mode 100644 index 0000000..a4cd3df --- /dev/null +++ b/R/data_book.R @@ -0,0 +1,356 @@ +#' DataSheet Class +#' +#' An R6 class to handle and manage a data frame with associated metadata, filters, and various settings. +#' +#' @section Methods: +#' \describe{ +#' \item{\code{set_data(new_data, messages, check_names)}}{Sets the data for the DataSheet object.} +#' \item{\code{set_changes(new_changes)}}{Sets the changes for the DataSheet object.} +#' \item{\code{set_filters(new_filters)}}{Sets the filters for the DataSheet object.} +#' \item{\code{set_column_selections(new_column_selections)}}{Sets the column selections for the DataSheet object.} +#' \item{\code{set_meta(new_meta)}}{Sets the metadata for the DataSheet object.} +#' \item{\code{clear_metadata()}}{Clears the metadata for the DataSheet object.} +#' \item{\code{clear_variables_metadata()}}{Clears the variables metadata for the DataSheet object.} +#' \item{\code{add_defaults_meta()}}{Adds default metadata to the DataSheet object.} +#' \item{\code{add_defaults_variables_metadata(column_names)}}{Adds default variables metadata to the DataSheet object.} +#' \item{\code{set_objects(new_objects)}}{Sets the objects for the DataSheet object.} +#' \item{\code{set_calculations(new_calculations)}}{Sets the calculations for the DataSheet object.} +#' \item{\code{set_keys(new_keys)}}{Sets the keys for the DataSheet object.} +#' \item{\code{set_comments(new_comments)}}{Sets the comments for the DataSheet object.} +#' \item{\code{append_to_metadata(label, value)}}{Appends to the metadata of the DataSheet object.} +#' \item{\code{is_metadata(label)}}{Checks if a label is in the metadata of the DataSheet object.} +#' } +#' +#' @section Active bindings: +#' \describe{ +#' \item{\code{data_changed}}{Logical, indicates if the data has changed. If setting a value, \code{new_value} must be \code{TRUE} or \code{FALSE}.} +#' \item{\code{metadata_changed}}{Logical, indicates if the metadata has changed. If setting a value, \code{new_value} must be \code{TRUE} or \code{FALSE}.} +#' \item{\code{variables_metadata_changed}}{Logical, indicates if the variables metadata has changed. If setting a value, \code{new_value} must be \code{TRUE} or \code{FALSE}.} +#' \item{\code{current_filter}}{A list representing the current filter. If setting a value, \code{filter} must be a list.} +#' \item{\code{current_column_selection}}{A list representing the current column selection. If setting a value, \code{column_selection} must be a list.} +#' } +#' +#' @field data A data frame to be managed. +#' @field filters A list of filters to be applied to the data. +#' @field column_selections A list of column selections. +#' @field objects A list of objects associated with the data. +#' @field keys A list of keys for the data. +#' @field comments A list of comments associated with the data. +#' @field calculations A list of calculations to be performed on the data. +#' @field changes A list of changes applied to the data. +#' @field .current_filter The current filter being applied. +#' @field .current_column_selection The current column selection being applied. +#' @field .data_changed Logical, indicates if the data has changed. +#' @field .metadata_changed Logical, indicates if the metadata has changed. +#' @field .variables_metadata_changed Logical, indicates if the variables metadata has changed. +#' @field .last_graph The last graph generated. +#' +#' @export +DataSheet <- R6::R6Class( + "DataSheet", + public = list( + #' @description + #' Create a new DataSheet object. + #' + #' @param data A data frame to be managed by the DataSheet object. Default is an empty data frame. + #' @param data_name A character string for the name of the data set. Default is an empty string. + #' @param variables_metadata A data frame containing metadata for the variables. Default is an empty data frame. + #' @param metadata A list containing additional metadata. Default is an empty list. + #' @param imported_from A character string indicating the source of the data import. Default is an empty string. + #' @param messages Logical, if TRUE messages will be shown during the setup. Default is TRUE. + #' @param convert Logical, if TRUE data will be converted. Default is TRUE. + #' @param create Logical, if TRUE the data will be created. Default is TRUE. + #' @param start_point Numeric, the starting point for default naming. Default is 1. + #' @param filters A list of filters to be applied to the data. Default is an empty list. + #' @param column_selections A list of column selections. Default is an empty list. + #' @param objects A list of objects associated with the data. Default is an empty list. + #' @param calculations A list of calculations to be performed on the data. Default is an empty list. + #' @param keys A list of keys for the data. Default is an empty list. + #' @param comments A list of comments associated with the data. Default is an empty list. + #' @param keep_attributes Logical, if TRUE attributes will be kept. Default is TRUE. + #' @return A new `DataSheet` object. + initialize = function(data = data.frame(), data_name = "", + variables_metadata = data.frame(), metadata = list(), + imported_from = "", + messages = TRUE, convert=TRUE, create = TRUE, + start_point=1, filters = list(), column_selections = list(), objects = list(), + calculations = list(), keys = list(), comments = list(), keep_attributes = TRUE) { + # Set up the data object + self$set_data(data, messages) + self$set_changes(list()) + #removed until this can be fixed. + #self$set_variables_metadata(variables_metadata) + + # Set first so that "no_filter" is added + self$set_filters(filters) + self$set_column_selections(column_selections) + if (keep_attributes) { + self$set_meta(c(attributes(private$data), metadata)) + } else { + self$set_meta(metadata) + self$clear_variables_metadata() + } + self$add_defaults_meta() + self$add_defaults_variables_metadata(self$get_column_names()) + #self$update_variables_metadata() + self$set_objects(objects) + self$set_calculations(calculations) + self$set_keys(keys) + self$set_comments(comments) + + # If no name for the data.frame has been given in the list we create a default one. + # Decide how to choose default name index + if (!(is.null(data_name) || data_name == "" || missing(data_name))) { + if (data_name != make.names(iconv(data_name, to = "ASCII//TRANSLIT", sub = "."))) { + message("data_name is invalid. It will be made valid automatically.") + data_name <- make.names(iconv(data_name, to = "ASCII//TRANSLIT", sub = ".")) + } + self$append_to_metadata(data_name_label, data_name) + } else if (!self$is_metadata(data_name_label)) { + if ((is.null(data_name) || data_name == "" || missing(data_name))) { + self$append_to_metadata(data_name_label, paste0("data_set_", sprintf("%03d", start_point))) + if (messages) { + message(paste0("No name specified in data_tables list for data frame ", start_point, ". + Data frame will have default name: ", "data_set_", sprintf("%03d", start_point))) + } + } else { + self$append_to_metadata(data_name_label, data_name) + } + } + }, + + #' @description + #' Sets the data for the DataSheet object. Accepts various data types and converts them to a data frame. + #' + #' @param new_data The new data to be set. It can be a matrix, tibble, data.table, ts object, array, or vector. + #' @param messages Logical, if TRUE, messages will be shown during the data setup. Default is TRUE. + #' @param check_names Logical, if TRUE, column names will be checked and made valid if necessary. Default is TRUE. + #' + #' @return The DataSheet object with the updated data. + set_data = function(new_data, messages = TRUE, check_names = TRUE) { + if (is.matrix(new_data)) new_data <- as.data.frame(new_data) + # This case could happen when removing rows + # as.data.frame preserves column and data frame attributes so no issue with this + else if (tibble::is_tibble(new_data) || data.table::is.data.table(new_data)) new_data <- as.data.frame(new_data) + # TODO convert ts objects correctly + else if (is.ts(new_data)) { + ind <- zoo::index(new_data) + new_data <- data.frame(index = ind, value = new_data) + } + else if (is.array(new_data)) { + new_data <- as.data.frame(new_data) + } + else if (is.vector(new_data) && !is.list(new_data)) { + new_data <- as.data.frame(new_data) + } + + if (!is.data.frame(new_data)) { + stop("Data set must be of type: data.frame") + } + else { + if (length(new_data) == 0 && messages) { + message("data is empty. Data will be an empty data frame.") + } + if (check_names) { + # "T" should be avoided as a column name but is not checked by make.names() + if ("T" %in% names(new_data)) names(new_data)[names(new_data) == "T"] <- ".T" + valid_names <- make.names(iconv(names(new_data), to = "ASCII//TRANSLIT", sub = "."), unique = TRUE) + if (!all(names(new_data) == valid_names)) { + warning("Not all column names are syntactically valid or unique. make.names() and iconv() will be used to force them to be valid and unique.") + names(new_data) <- valid_names + } + } + private$data <- new_data + self$append_to_changes(list(Set_property, "data")) + self$data_changed <- TRUE + self$variables_metadata_changed <- TRUE + } + }, + + #' @description + #' Sets the metadata for the DataSheet object. + #' @param new_meta A list containing the new metadata. + set_meta = function(new_meta) { + meta_data_copy <- new_meta + self$clear_metadata() + if (!is.list(meta_data_copy)) stop("new_meta must be of type: list") + for (name in names(meta_data_copy)) { + self$append_to_metadata(name, meta_data_copy[[name]]) + } + self$metadata_changed <- TRUE + self$append_to_changes(list(Set_property, "meta data")) + }, + + #' @description + #' Clears the metadata for the DataSheet object. + clear_metadata = function() { + for (name in names(attributes(private$data))) { + if (!name %in% c(data_type_label, data_name_label, "row.names", "names")) attr(private$data, name) <- NULL + } + self$add_defaults_meta() + self$metadata_changed <- TRUE + self$append_to_changes(list(Set_property, "meta data")) + }, + + #' @description + #' Sets the changes for the DataSheet object. + #' @param new_changes A list containing the new changes. + set_changes = function(new_changes) { + if (!is.list(new_changes)) stop("Changes must be of type: list") + private$changes <- new_changes + self$append_to_changes(list(Set_property, "changes")) + }, + + #' @description + #' Sets the filters for the DataSheet object. + #' @param new_filters A list containing the new filters. + set_filters = function(new_filters) { + if (!is.list(new_filters)) stop("Filters must be of type: list") + self$append_to_changes(list(Set_property, "filters")) + private$filters <- new_filters + if (!"no_filter" %in% names(private$filters)) { + self$add_filter(filter = list(), filter_name = "no_filter", replace = TRUE, set_as_current = TRUE, na.rm = FALSE, is_no_filter = TRUE) + } + }, + + #' @description + #' Sets the column selections for the DataSheet object. + #' @param new_column_selections A list containing the new column selections. + set_column_selections = function(new_column_selections) { + stopifnot(is.list(new_column_selections)) + self$append_to_changes(list(Set_property, "column selections")) + private$column_selections <- new_column_selections + if (!".everything" %in% names(private$column_selections)) { + self$add_column_selection(column_selection = list(), name = ".everything", replace = TRUE, set_as_current = TRUE, is_everything = TRUE) + } + }, + + #' @description + #' Sets the objects for the DataSheet object. + #' @param new_objects A list containing the new objects. + set_objects = function(new_objects) { + if (!is.list(new_objects)) stop("new_objects must be of type: list") + self$append_to_changes(list(Set_property, "objects")) + private$objects <- new_objects + }, + + #' @description + #' Sets the calculations for the DataSheet object. + #' @param new_calculations A list containing the new calculations. + set_calculations = function(new_calculations) { + if (!is.list(new_calculations)) stop("new_calculations must be of type: list") + self$append_to_changes(list(Set_property, "calculations")) + private$calculations <- new_calculations + }, + + #' @description + #' Sets the keys for the DataSheet object. + #' @param new_keys A list containing the new keys. + set_keys = function(new_keys) { + if (!is.list(new_keys)) stop("new_keys must be of type: list") + self$append_to_changes(list(Set_property, "keys")) + private$keys <- new_keys + }, + + #' @description + #' Sets the comments for the DataSheet object. + #' @param new_comments A list containing the new comments. + set_comments = function(new_comments) { + if (!is.list(new_comments)) stop("new_comments must be of type: list") + self$append_to_changes(list(Set_property, "comments")) + private$comments <- new_comments + } + ), + + private = list( + #' @field data A data frame to be managed. + data = data.frame(), + #' @field filters A list of filters to be applied to the data. + filters = list(), + #' @field column_selections A list of column selections. + column_selections = list(), + #' @field objects A list of objects associated with the data. + objects = list(), + #' @field keys A list of keys for the data. + keys = list(), + #' @field comments A list of comments associated with the data. + comments = list(), + #' @field calculations A list of calculations to be performed on the data. + calculations = list(), + #' @field changes A list of changes applied to the data. + changes = list(), + #' @field .current_filter The current filter being applied. + .current_filter = list(), + #' @field .current_column_selection The current column selection being applied. + .current_column_selection = list(), + #' @field .data_changed Logical, indicates if the data has changed. + .data_changed = FALSE, + #' @field .metadata_changed Logical, indicates if the metadata has changed. + .metadata_changed = FALSE, + #' @field .variables_metadata_changed Logical, indicates if the variables metadata has changed. + .variables_metadata_changed = FALSE, + #' @field .last_graph The last graph generated. + .last_graph = NULL + ), + + active = list( + #' @field data_changed Logical, indicates if the data has changed. + #' If setting a value, new_value must be TRUE or FALSE. + data_changed = function(new_value) { + if (missing(new_value)) return(private$.data_changed) + else { + if (new_value != TRUE && new_value != FALSE) stop("new_val must be TRUE or FALSE") + private$.data_changed <- new_value + self$append_to_changes(list(Set_property, "data_changed")) + } + }, + + #' @field metadata_changed Logical, indicates if the metadata has changed. + #' If setting a value, new_value must be TRUE or FALSE. + metadata_changed = function(new_value) { + if (missing(new_value)) return(private$.metadata_changed) + else { + if (new_value != TRUE && new_value != FALSE) stop("new_val must be TRUE or FALSE") + private$.metadata_changed <- new_value + self$append_to_changes(list(Set_property, "metadata_changed")) + } + }, + + #' @field variables_metadata_changed Logical, indicates if the variables metadata has changed. + #' If setting a value, new_value must be TRUE or FALSE. + variables_metadata_changed = function(new_value) { + if (missing(new_value)) return(private$.variables_metadata_changed) + else { + if (new_value != TRUE && new_value != FALSE) stop("new_val must be TRUE or FALSE") + private$.variables_metadata_changed <- new_value + self$append_to_changes(list(Set_property, "variable_data_changed")) + } + }, + + #' @field current_filter A list representing the current filter. + #' If setting a value, filter must be a list. + current_filter = function(filter) { + if (missing(filter)) { + return(self$get_filter_as_logical(private$.current_filter$name)) + } else { + private$.current_filter <- filter + self$data_changed <- TRUE + self$append_to_changes(list(Set_property, "current_filter")) + } + }, + + #' @field current_column_selection A list representing the current column selection. + #' If setting a value, column_selection must be a list. + current_column_selection = function(column_selection) { + if (missing(column_selection)) { + if (!is.null(private$.current_column_selection)) { + return(self$get_column_selection_column_names(private$.current_column_selection$name)) + } else return(names(private$data)) + } else { + private$.current_column_selection <- column_selection + self$data_changed <- TRUE + self$append_to_changes(list(Set_property, "current_column_selection")) + } + } + ) +) diff --git a/data_book.R b/data_book.R new file mode 100644 index 0000000..0e68e34 --- /dev/null +++ b/data_book.R @@ -0,0 +1,195 @@ +#' DataSheet Class +#' +#' An R6 class to handle and manage a data frame with associated metadata, filters, and various settings. +#' +#' @section Methods: +#' \describe{ +#' \item{\code{set_data(data, messages)}}{Sets the data for the DataSheet object.} +#' \item{\code{set_changes(changes)}}{Sets the changes for the DataSheet object.} +#' \item{\code{set_filters(filters)}}{Sets the filters for the DataSheet object.} +#' \item{\code{set_column_selections(column_selections)}}{Sets the column selections for the DataSheet object.} +#' \item{\code{set_meta(meta)}}{Sets the metadata for the DataSheet object.} +#' \item{\code{clear_variables_metadata()}}{Clears the variables metadata for the DataSheet object.} +#' \item{\code{add_defaults_meta()}}{Adds default metadata to the DataSheet object.} +#' \item{\code{add_defaults_variables_metadata(column_names)}}{Adds default variables metadata to the DataSheet object.} +#' \item{\code{set_objects(objects)}}{Sets the objects for the DataSheet object.} +#' \item{\code{set_calculations(calculations)}}{Sets the calculations for the DataSheet object.} +#' \item{\code{set_keys(keys)}}{Sets the keys for the DataSheet object.} +#' \item{\code{set_comments(comments)}}{Sets the comments for the DataSheet object.} +#' \item{\code{append_to_metadata(label, value)}}{Appends to the metadata of the DataSheet object.} +#' \item{\code{is_metadata(label)}}{Checks if a label is in the metadata of the DataSheet object.} +#' } +#' +#' @section Active bindings: +#' \describe{ +#' \item{\code{data_changed}}{Indicates if the data has changed. When setting, the new value must be \code{TRUE} or \code{FALSE}.} +#' \item{\code{metadata_changed}}{Indicates if the metadata has changed. When setting, the new value must be \code{TRUE} or \code{FALSE}.} +#' \item{\code{variables_metadata_changed}}{Indicates if the variables metadata has changed. When setting, the new value must be \code{TRUE} or \code{FALSE}.} +#' \item{\code{current_filter}}{Represents the current filter.} +#' \item{\code{current_column_selection}}{Represents the current column selection.} +#' } +#' +#' @export +DataSheet <- R6::R6Class( + "DataSheet", + public = list( + #' @description + #' Create a new DataSheet object. + #' + #' @param data A data frame to be managed by the DataSheet object. Default is an empty data frame. + #' @param data_name A character string for the name of the data set. Default is an empty string. + #' @param variables_metadata A data frame containing metadata for the variables. Default is an empty data frame. + #' @param metadata A list containing additional metadata. Default is an empty list. + #' @param imported_from A character string indicating the source of the data import. Default is an empty string. + #' @param messages Logical, if TRUE messages will be shown during the setup. Default is TRUE. + #' @param convert Logical, if TRUE data will be converted. Default is TRUE. + #' @param create Logical, if TRUE the data will be created. Default is TRUE. + #' @param start_point Numeric, the starting point for default naming. Default is 1. + #' @param filters A list of filters to be applied to the data. Default is an empty list. + #' @param column_selections A list of column selections. Default is an empty list. + #' @param objects A list of objects associated with the data. Default is an empty list. + #' @param calculations A list of calculations to be performed on the data. Default is an empty list. + #' @param keys A list of keys for the data. Default is an empty list. + #' @param comments A list of comments associated with the data. Default is an empty list. + #' @param keep_attributes Logical, if TRUE attributes will be kept. Default is TRUE. + #' @return A new `DataSheet` object. + initialize = function(data = data.frame(), data_name = "", + variables_metadata = data.frame(), metadata = list(), + imported_from = "", + messages = TRUE, convert=TRUE, create = TRUE, + start_point=1, filters = list(), column_selections = list(), objects = list(), + calculations = list(), keys = list(), comments = list(), keep_attributes = TRUE) { + # Set up the data object + self$set_data(data, messages) + self$set_changes(list()) + #removed until this can be fixed. + #self$set_variables_metadata(variables_metadata) + + # Set first so that "no_filter" is added + self$set_filters(filters) + self$set_column_selections(column_selections) + if(keep_attributes) { + self$set_meta(c(attributes(private$data), metadata)) + } + else { + self$set_meta(metadata) + self$clear_variables_metadata() + } + self$add_defaults_meta() + self$add_defaults_variables_metadata(self$get_column_names()) + #self$update_variables_metadata() + self$set_objects(objects) + self$set_calculations(calculations) + self$set_keys(keys) + self$set_comments(comments) + + # If no name for the data.frame has been given in the list we create a default one. + # Decide how to choose default name index + if ( !(is.null(data_name) || data_name == "" || missing(data_name))) { + if(data_name != make.names(iconv(data_name, to = "ASCII//TRANSLIT", sub = "."))) { + message("data_name is invalid. It will be made valid automatically.") + data_name <- make.names(iconv(data_name, to = "ASCII//TRANSLIT", sub = ".")) + } + self$append_to_metadata(data_name_label, data_name) + } + else if (!self$is_metadata(data_name_label)) { + if (( is.null(data_name) || data_name == "" || missing(data_name))) { + self$append_to_metadata(data_name_label,paste0("data_set_",sprintf("%03d", start_point))) + if (messages) { + message(paste0("No name specified in data_tables list for data frame ", start_point, ". + Data frame will have default name: ", "data_set_",sprintf("%03d", start_point))) + } + } + else self$append_to_metadata(data_name_label, data_name) + } + } + ), + private = list( + #' @field data A data frame to be managed. + data = data.frame(), + #' @field filters A list of filters to be applied to the data. + filters = list(), + #' @field column_selections A list of column selections. + column_selections = list(), + #' @field objects A list of objects associated with the data. + objects = list(), + #' @field keys A list of keys for the data. + keys = list(), + #' @field comments A list of comments associated with the data. + comments = list(), + #' @field calculations A list of calculations to be performed on the data. + calculations = list(), + #' @field changes A list of changes applied to the data. + changes = list(), + #' @field .current_filter The current filter being applied. + .current_filter = list(), + #' @field .current_column_selection The current column selection being applied. + .current_column_selection = list(), + #' @field .data_changed Logical, indicates if the data has changed. + .data_changed = FALSE, + #' @field .metadata_changed Logical, indicates if the metadata has changed. + .metadata_changed = FALSE, + #' @field .variables_metadata_changed Logical, indicates if the variables metadata has changed. + .variables_metadata_changed = FALSE, + #' @field .last_graph The last graph generated. + .last_graph = NULL + ), + active = list( + #' @field data_changed Logical, indicates if the data has changed. + #' @param new_value Logical, if \code{TRUE} data has changed, if \code{FALSE} it has not. + data_changed = function(new_value) { + if(missing(new_value)) return(private$.data_changed) + else { + if(new_value != TRUE && new_value != FALSE) stop("new_val must be TRUE or FALSE") + private$.data_changed <- new_value + self$append_to_changes(list(Set_property, "data_changed")) + } + }, + #' @field metadata_changed Logical, indicates if the metadata has changed. + #' @param new_value Logical, if \code{TRUE} metadata has changed, if \code{FALSE} it has not. + metadata_changed = function(new_value) { + if(missing(new_value)) return(private$.metadata_changed) + else { + if(new_value != TRUE && new_value != FALSE) stop("new_val must be TRUE or FALSE") + private$.metadata_changed <- new_value + self$append_to_changes(list(Set_property, "metadata_changed")) + } + }, + #' @field variables_metadata_changed Logical, indicates if the variables metadata has changed. + #' @param new_value Logical, if \code{TRUE} variables metadata has changed, if \code{FALSE} it has not. + variables_metadata_changed = function(new_value) { + if(missing(new_value)) return(private$.variables_metadata_changed) + else { + if(new_value != TRUE && new_value != FALSE) stop("new_val must be TRUE or FALSE") + private$.variables_metadata_changed <- new_value + self$append_to_changes(list(Set_property, "variable_data_changed")) + } + }, + #' @field current_filter A list representing the current filter. + #' @param filter A list specifying the filter to be set. + current_filter = function(filter) { + if(missing(filter)) { + return(self$get_filter_as_logical(private$.current_filter$name)) + } + else { + private$.current_filter <- filter + self$data_changed <- TRUE + self$append_to_changes(list(Set_property, "current_filter")) + } + }, + #' @field current_column_selection A list representing the current column selection. + #' @param column_selection A list specifying the column selection to be set. + current_column_selection = function(column_selection) { + if(missing(column_selection)) { + if (!is.null(private$.current_column_selection)) { + return(self$get_column_selection_column_names(private$.current_column_selection$name)) + } else return(names(private$data)) + } + else { + private$.current_column_selection <- column_selection + self$data_changed <- TRUE + self$append_to_changes(list(Set_property, "current_column_selection")) + } + } + ) +) diff --git a/databook.Rproj b/databook.Rproj index 8e3c2eb..21a4da0 100644 --- a/databook.Rproj +++ b/databook.Rproj @@ -11,3 +11,7 @@ Encoding: UTF-8 RnwWeave: Sweave LaTeX: pdfLaTeX + +BuildType: Package +PackageUseDevtools: Yes +PackageInstallArgs: --no-multiarch --with-keep.source diff --git a/man/DataSheet.Rd b/man/DataSheet.Rd new file mode 100644 index 0000000..a0430a4 --- /dev/null +++ b/man/DataSheet.Rd @@ -0,0 +1,454 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/data_book.R +\name{DataSheet} +\alias{DataSheet} +\title{DataSheet Class} +\description{ +DataSheet Class + +DataSheet Class +} +\details{ +An R6 class to handle and manage a data frame with associated metadata, filters, and various settings. +} +\section{Methods}{ + +\describe{ +\item{\code{set_data(new_data, messages, check_names)}}{Sets the data for the DataSheet object.} +\item{\code{set_changes(new_changes)}}{Sets the changes for the DataSheet object.} +\item{\code{set_filters(new_filters)}}{Sets the filters for the DataSheet object.} +\item{\code{set_column_selections(new_column_selections)}}{Sets the column selections for the DataSheet object.} +\item{\code{set_meta(new_meta)}}{Sets the metadata for the DataSheet object.} +\item{\code{clear_metadata()}}{Clears the metadata for the DataSheet object.} +\item{\code{clear_variables_metadata()}}{Clears the variables metadata for the DataSheet object.} +\item{\code{add_defaults_meta()}}{Adds default metadata to the DataSheet object.} +\item{\code{add_defaults_variables_metadata(column_names)}}{Adds default variables metadata to the DataSheet object.} +\item{\code{set_objects(new_objects)}}{Sets the objects for the DataSheet object.} +\item{\code{set_calculations(new_calculations)}}{Sets the calculations for the DataSheet object.} +\item{\code{set_keys(new_keys)}}{Sets the keys for the DataSheet object.} +\item{\code{set_comments(new_comments)}}{Sets the comments for the DataSheet object.} +\item{\code{append_to_metadata(label, value)}}{Appends to the metadata of the DataSheet object.} +\item{\code{is_metadata(label)}}{Checks if a label is in the metadata of the DataSheet object.} +} +} + +\section{Active bindings}{ + +\describe{ +\item{\code{data_changed}}{Logical, indicates if the data has changed. If setting a value, \code{new_value} must be \code{TRUE} or \code{FALSE}.} +\item{\code{metadata_changed}}{Logical, indicates if the metadata has changed. If setting a value, \code{new_value} must be \code{TRUE} or \code{FALSE}.} +\item{\code{variables_metadata_changed}}{Logical, indicates if the variables metadata has changed. If setting a value, \code{new_value} must be \code{TRUE} or \code{FALSE}.} +\item{\code{current_filter}}{A list representing the current filter. If setting a value, \code{filter} must be a list.} +\item{\code{current_column_selection}}{A list representing the current column selection. If setting a value, \code{column_selection} must be a list.} +} +} + +\section{Public fields}{ +\if{html}{\out{
}} +\describe{ +\item{\code{data}}{A data frame to be managed.} + +\item{\code{filters}}{A list of filters to be applied to the data.} + +\item{\code{column_selections}}{A list of column selections.} + +\item{\code{objects}}{A list of objects associated with the data.} + +\item{\code{keys}}{A list of keys for the data.} + +\item{\code{comments}}{A list of comments associated with the data.} + +\item{\code{calculations}}{A list of calculations to be performed on the data.} + +\item{\code{changes}}{A list of changes applied to the data.} + +\item{\code{.current_filter}}{The current filter being applied.} + +\item{\code{.current_column_selection}}{The current column selection being applied.} + +\item{\code{.data_changed}}{Logical, indicates if the data has changed.} + +\item{\code{.metadata_changed}}{Logical, indicates if the metadata has changed.} + +\item{\code{.variables_metadata_changed}}{Logical, indicates if the variables metadata has changed.} + +\item{\code{.last_graph}}{The last graph generated.} + +\item{\code{data}}{A data frame to be managed.} + +\item{\code{filters}}{A list of filters to be applied to the data.} + +\item{\code{column_selections}}{A list of column selections.} + +\item{\code{objects}}{A list of objects associated with the data.} + +\item{\code{keys}}{A list of keys for the data.} + +\item{\code{comments}}{A list of comments associated with the data.} + +\item{\code{calculations}}{A list of calculations to be performed on the data.} + +\item{\code{changes}}{A list of changes applied to the data.} + +\item{\code{.current_filter}}{The current filter being applied.} + +\item{\code{.current_column_selection}}{The current column selection being applied.} + +\item{\code{.data_changed}}{Logical, indicates if the data has changed.} + +\item{\code{.metadata_changed}}{Logical, indicates if the metadata has changed.} + +\item{\code{.variables_metadata_changed}}{Logical, indicates if the variables metadata has changed.} + +\item{\code{.last_graph}}{The last graph generated.} +} +\if{html}{\out{
}} +} +\section{Active bindings}{ +\if{html}{\out{
}} +\describe{ +\item{\code{data}}{A data frame to be managed.} + +\item{\code{filters}}{A list of filters to be applied to the data.} + +\item{\code{column_selections}}{A list of column selections.} + +\item{\code{objects}}{A list of objects associated with the data.} + +\item{\code{keys}}{A list of keys for the data.} + +\item{\code{comments}}{A list of comments associated with the data.} + +\item{\code{calculations}}{A list of calculations to be performed on the data.} + +\item{\code{changes}}{A list of changes applied to the data.} + +\item{\code{.current_filter}}{The current filter being applied.} + +\item{\code{.current_column_selection}}{The current column selection being applied.} + +\item{\code{.data_changed}}{Logical, indicates if the data has changed.} + +\item{\code{.metadata_changed}}{Logical, indicates if the metadata has changed.} + +\item{\code{.variables_metadata_changed}}{Logical, indicates if the variables metadata has changed.} + +\item{\code{.last_graph}}{The last graph generated.} + +\item{\code{data}}{A data frame to be managed.} + +\item{\code{filters}}{A list of filters to be applied to the data.} + +\item{\code{column_selections}}{A list of column selections.} + +\item{\code{objects}}{A list of objects associated with the data.} + +\item{\code{keys}}{A list of keys for the data.} + +\item{\code{comments}}{A list of comments associated with the data.} + +\item{\code{calculations}}{A list of calculations to be performed on the data.} + +\item{\code{changes}}{A list of changes applied to the data.} + +\item{\code{.current_filter}}{The current filter being applied.} + +\item{\code{.current_column_selection}}{The current column selection being applied.} + +\item{\code{.data_changed}}{Logical, indicates if the data has changed.} + +\item{\code{.metadata_changed}}{Logical, indicates if the metadata has changed.} + +\item{\code{.variables_metadata_changed}}{Logical, indicates if the variables metadata has changed.} + +\item{\code{.last_graph}}{The last graph generated.} + +\item{\code{data_changed}}{Logical, indicates if the data has changed. +If setting a value, new_value must be TRUE or FALSE.} + +\item{\code{metadata_changed}}{Logical, indicates if the metadata has changed. +If setting a value, new_value must be TRUE or FALSE.} + +\item{\code{variables_metadata_changed}}{Logical, indicates if the variables metadata has changed. +If setting a value, new_value must be TRUE or FALSE.} + +\item{\code{current_filter}}{A list representing the current filter. +If setting a value, filter must be a list.} + +\item{\code{current_column_selection}}{A list representing the current column selection. +If setting a value, column_selection must be a list.} +} +\if{html}{\out{
}} +} +\section{Methods}{ +\subsection{Public methods}{ +\itemize{ +\item \href{#method-DataSheet-new}{\code{DataSheet$new()}} +\item \href{#method-DataSheet-set_data}{\code{DataSheet$set_data()}} +\item \href{#method-DataSheet-set_meta}{\code{DataSheet$set_meta()}} +\item \href{#method-DataSheet-clear_metadata}{\code{DataSheet$clear_metadata()}} +\item \href{#method-DataSheet-set_changes}{\code{DataSheet$set_changes()}} +\item \href{#method-DataSheet-set_filters}{\code{DataSheet$set_filters()}} +\item \href{#method-DataSheet-set_column_selections}{\code{DataSheet$set_column_selections()}} +\item \href{#method-DataSheet-set_objects}{\code{DataSheet$set_objects()}} +\item \href{#method-DataSheet-set_calculations}{\code{DataSheet$set_calculations()}} +\item \href{#method-DataSheet-set_keys}{\code{DataSheet$set_keys()}} +\item \href{#method-DataSheet-set_comments}{\code{DataSheet$set_comments()}} +\item \href{#method-DataSheet-clone}{\code{DataSheet$clone()}} +} +} +\if{html}{\out{
}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-DataSheet-new}{}}} +\subsection{Method \code{new()}}{ +Create a new DataSheet object. +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{DataSheet$new( + data = data.frame(), + data_name = "", + variables_metadata = data.frame(), + metadata = list(), + imported_from = "", + messages = TRUE, + convert = TRUE, + create = TRUE, + start_point = 1, + filters = list(), + column_selections = list(), + objects = list(), + calculations = list(), + keys = list(), + comments = list(), + keep_attributes = TRUE +)}\if{html}{\out{
}} +} + +\subsection{Arguments}{ +\if{html}{\out{
}} +\describe{ +\item{\code{data}}{A data frame to be managed by the DataSheet object. Default is an empty data frame.} + +\item{\code{data_name}}{A character string for the name of the data set. Default is an empty string.} + +\item{\code{variables_metadata}}{A data frame containing metadata for the variables. Default is an empty data frame.} + +\item{\code{metadata}}{A list containing additional metadata. Default is an empty list.} + +\item{\code{imported_from}}{A character string indicating the source of the data import. Default is an empty string.} + +\item{\code{messages}}{Logical, if TRUE messages will be shown during the setup. Default is TRUE.} + +\item{\code{convert}}{Logical, if TRUE data will be converted. Default is TRUE.} + +\item{\code{create}}{Logical, if TRUE the data will be created. Default is TRUE.} + +\item{\code{start_point}}{Numeric, the starting point for default naming. Default is 1.} + +\item{\code{filters}}{A list of filters to be applied to the data. Default is an empty list.} + +\item{\code{column_selections}}{A list of column selections. Default is an empty list.} + +\item{\code{objects}}{A list of objects associated with the data. Default is an empty list.} + +\item{\code{calculations}}{A list of calculations to be performed on the data. Default is an empty list.} + +\item{\code{keys}}{A list of keys for the data. Default is an empty list.} + +\item{\code{comments}}{A list of comments associated with the data. Default is an empty list.} + +\item{\code{keep_attributes}}{Logical, if TRUE attributes will be kept. Default is TRUE.} +} +\if{html}{\out{
}} +} +\subsection{Returns}{ +A new \code{DataSheet} object. +} +} +\if{html}{\out{
}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-DataSheet-set_data}{}}} +\subsection{Method \code{set_data()}}{ +Sets the data for the DataSheet object. Accepts various data types and converts them to a data frame. +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{DataSheet$set_data(new_data, messages = TRUE, check_names = TRUE)}\if{html}{\out{
}} +} + +\subsection{Arguments}{ +\if{html}{\out{
}} +\describe{ +\item{\code{new_data}}{The new data to be set. It can be a matrix, tibble, data.table, ts object, array, or vector.} + +\item{\code{messages}}{Logical, if TRUE, messages will be shown during the data setup. Default is TRUE.} + +\item{\code{check_names}}{Logical, if TRUE, column names will be checked and made valid if necessary. Default is TRUE.} +} +\if{html}{\out{
}} +} +\subsection{Returns}{ +The DataSheet object with the updated data. +} +} +\if{html}{\out{
}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-DataSheet-set_meta}{}}} +\subsection{Method \code{set_meta()}}{ +Sets the metadata for the DataSheet object. +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{DataSheet$set_meta(new_meta)}\if{html}{\out{
}} +} + +\subsection{Arguments}{ +\if{html}{\out{
}} +\describe{ +\item{\code{new_meta}}{A list containing the new metadata.} +} +\if{html}{\out{
}} +} +} +\if{html}{\out{
}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-DataSheet-clear_metadata}{}}} +\subsection{Method \code{clear_metadata()}}{ +Clears the metadata for the DataSheet object. +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{DataSheet$clear_metadata()}\if{html}{\out{
}} +} + +} +\if{html}{\out{
}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-DataSheet-set_changes}{}}} +\subsection{Method \code{set_changes()}}{ +Sets the changes for the DataSheet object. +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{DataSheet$set_changes(new_changes)}\if{html}{\out{
}} +} + +\subsection{Arguments}{ +\if{html}{\out{
}} +\describe{ +\item{\code{new_changes}}{A list containing the new changes.} +} +\if{html}{\out{
}} +} +} +\if{html}{\out{
}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-DataSheet-set_filters}{}}} +\subsection{Method \code{set_filters()}}{ +Sets the filters for the DataSheet object. +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{DataSheet$set_filters(new_filters)}\if{html}{\out{
}} +} + +\subsection{Arguments}{ +\if{html}{\out{
}} +\describe{ +\item{\code{new_filters}}{A list containing the new filters.} +} +\if{html}{\out{
}} +} +} +\if{html}{\out{
}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-DataSheet-set_column_selections}{}}} +\subsection{Method \code{set_column_selections()}}{ +Sets the column selections for the DataSheet object. +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{DataSheet$set_column_selections(new_column_selections)}\if{html}{\out{
}} +} + +\subsection{Arguments}{ +\if{html}{\out{
}} +\describe{ +\item{\code{new_column_selections}}{A list containing the new column selections.} +} +\if{html}{\out{
}} +} +} +\if{html}{\out{
}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-DataSheet-set_objects}{}}} +\subsection{Method \code{set_objects()}}{ +Sets the objects for the DataSheet object. +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{DataSheet$set_objects(new_objects)}\if{html}{\out{
}} +} + +\subsection{Arguments}{ +\if{html}{\out{
}} +\describe{ +\item{\code{new_objects}}{A list containing the new objects.} +} +\if{html}{\out{
}} +} +} +\if{html}{\out{
}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-DataSheet-set_calculations}{}}} +\subsection{Method \code{set_calculations()}}{ +Sets the calculations for the DataSheet object. +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{DataSheet$set_calculations(new_calculations)}\if{html}{\out{
}} +} + +\subsection{Arguments}{ +\if{html}{\out{
}} +\describe{ +\item{\code{new_calculations}}{A list containing the new calculations.} +} +\if{html}{\out{
}} +} +} +\if{html}{\out{
}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-DataSheet-set_keys}{}}} +\subsection{Method \code{set_keys()}}{ +Sets the keys for the DataSheet object. +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{DataSheet$set_keys(new_keys)}\if{html}{\out{
}} +} + +\subsection{Arguments}{ +\if{html}{\out{
}} +\describe{ +\item{\code{new_keys}}{A list containing the new keys.} +} +\if{html}{\out{
}} +} +} +\if{html}{\out{
}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-DataSheet-set_comments}{}}} +\subsection{Method \code{set_comments()}}{ +Sets the comments for the DataSheet object. +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{DataSheet$set_comments(new_comments)}\if{html}{\out{
}} +} + +\subsection{Arguments}{ +\if{html}{\out{
}} +\describe{ +\item{\code{new_comments}}{A list containing the new comments.} +} +\if{html}{\out{
}} +} +} +\if{html}{\out{
}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-DataSheet-clone}{}}} +\subsection{Method \code{clone()}}{ +The objects of this class are cloneable with this method. +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{DataSheet$clone(deep = FALSE)}\if{html}{\out{
}} +} + +\subsection{Arguments}{ +\if{html}{\out{
}} +\describe{ +\item{\code{deep}}{Whether to make a deep clone.} +} +\if{html}{\out{
}} +} +} +}