Skip to content

Commit

Permalink
Rename to safeframe
Browse files Browse the repository at this point in the history
  • Loading branch information
chartgerink committed Nov 11, 2024
1 parent 48e5518 commit 63552c2
Show file tree
Hide file tree
Showing 61 changed files with 717 additions and 716 deletions.
484 changes: 242 additions & 242 deletions CITATION.cff

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Package: datatagr
Package: safeframe
Title: Generic Data Labelling and Validating
Version: 0.0.1
Authors@R: c(
Expand All @@ -7,10 +7,10 @@ Authors@R: c(
person("Hugo", "Gruson", , "[email protected]", role = "rev",
comment = c(ORCID = "0000-0002-4094-1476"))
)
Description: Provides tools to help label and validate data according to user-specified rules. The 'datatagr' class adds variable level attributes to 'data.frame' columns. Once labelled, these variables can be seamlessly used in downstream analyses, making data pipelines clearer, more robust, and more reliable.
Description: Provides tools to help label and validate data according to user-specified rules. The 'safeframe' class adds variable level attributes to 'data.frame' columns. Once labelled, these variables can be seamlessly used in downstream analyses, making data pipelines clearer, more robust, and more reliable.
License: MIT + file LICENSE
URL: https://epiverse-trace.github.io/datatagr/, https://github.com/epiverse-trace/datatagr
BugReports: https://github.com/epiverse-trace/datatagr/issues
URL: https://epiverse-trace.github.io/safeframe/, https://github.com/epiverse-trace/safeframe
BugReports: https://github.com/epiverse-trace/safeframe/issues
Depends:
R (>= 3.1.0)
Imports:
Expand Down
16 changes: 8 additions & 8 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# Generated by roxygen2: do not edit by hand

S3method("$<-",datatagr)
S3method("[",datatagr)
S3method("[<-",datatagr)
S3method("[[<-",datatagr)
S3method("names<-",datatagr)
S3method(print,datatagr)
S3method("$<-",safeframe)
S3method("[",safeframe)
S3method("[<-",safeframe)
S3method("[[<-",safeframe)
S3method("names<-",safeframe)
S3method(print,safeframe)
export(get_lost_labels_action)
export(has_label)
export(labels)
export(labels_df)
export(lost_labels_action)
export(make_datatagr)
export(make_safeframe)
export(set_labels)
export(type)
export(validate_datatagr)
export(validate_labels)
export(validate_safeframe)
export(validate_types)
importFrom(lifecycle,deprecated)
12 changes: 6 additions & 6 deletions R/drop_datatagr.R → R/drop_safeframe.R
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#' Remove the datatagr class from an object
#' Remove the safeframe class from an object
#'
#' Internal function. Used for dispatching to other methods when `NextMethod` is
#' an issue (typically to pass additional arguments to the `datatagr` method).
#' an issue (typically to pass additional arguments to the `safeframe` method).
#'
#' @param x a `datatagr` object
#' @param x a `safeframe` object
#'
#' @param remove_labels a `logical` indicating if labels should be removed from
#' the attributes; defaults to `TRUE`
#'
#' @noRd
#'
#' @return The function returns the object without the `datatagr` class.
#' @return The function returns the object without the `safeframe` class.
#'
drop_datatagr <- function(x, remove_labels = TRUE) {
drop_safeframe <- function(x, remove_labels = TRUE) {
classes <- class(x)
class(x) <- setdiff(classes, "datatagr")
class(x) <- setdiff(classes, "safeframe")
if (remove_labels) {
# Set the label attribute to NULL for all variables in x
for (var in names(x)) {
Expand Down
6 changes: 3 additions & 3 deletions R/has_label.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
#' @returns A numeric vector containing the position of the columns with the
#' requested labels
#'
#' @note Using this in a pipeline results in a 'datatagr' object, but does not
#' @note Using this in a pipeline results in a 'safeframe' object, but does not
#' maintain the variable labels at this time. It is primarily useful to make
#' your pipelines human readable.
#'
#' @export
#'
#' @examples
#' ## create datatagr
#' x <- make_datatagr(cars,
#' ## create safeframe
#' x <- make_safeframe(cars,
#' speed = "Miles per hour",
#' dist = "Distance in miles"
#' )
Expand Down
12 changes: 6 additions & 6 deletions R/labels.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#' Get the list of labels in a datatagr
#' Get the list of labels in a safeframe
#'
#' This function returns the list of labels identifying specific variable types
#' in a `datatagr` object.
#' in a `safeframe` object.
#'
#' @param x a `datatagr` object
#' @param x a `safeframe` object
#'
#' @param show_null a `logical` indicating if the complete list of labels,
#' including `NULL` ones, should be returned; if `FALSE`, only labels with a
Expand All @@ -18,16 +18,16 @@
#'
#' @examples
#'
#' ## make a datatagr
#' x <- make_datatagr(cars, speed = "Miles per hour")
#' ## make a safeframe
#' x <- make_safeframe(cars, speed = "Miles per hour")
#'
#' ## check non-null labels
#' labels(x)
#'
#' ## get a list of all labels, including NULL ones
#' labels(x, TRUE)
labels <- function(x, show_null = FALSE) {
checkmate::assertClass(x, "datatagr")
checkmate::assertClass(x, "safeframe")
out <- lapply(names(x), FUN = function(var) {
attr(x[[var]], "label")
})
Expand Down
12 changes: 6 additions & 6 deletions R/labels_df.R
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
#' Extract a data.frame of all labelled variables
#'
#' This function returns a `data.frame`, where labelled variables (as stored in
#' the `datatagr` object) are renamed. Note that the output is no longer a
#' `datatagr`, but a regular `data.frame`. Unlabeled variables are unaffected.
#' the `safeframe` object) are renamed. Note that the output is no longer a
#' `safeframe`, but a regular `data.frame`. Unlabeled variables are unaffected.
#'
#' @param x a `datatagr` object
#' @param x a `safeframe` object
#'
#' @export
#'
#' @return A `data.frame` of with variables renamed according to their labels.
#'
#' @examples
#'
#' x <- make_datatagr(cars,
#' x <- make_safeframe(cars,
#' speed = "Miles per hour",
#' dist = "Distance in miles"
#' )
#'
#' ## get a data.frame with variables renamed based on labels
#' labels_df(x)
labels_df <- function(x) {
checkmate::assertClass(x, "datatagr")
checkmate::assertClass(x, "safeframe")

labels <- unlist(labels(x))
out <- drop_datatagr(x)
out <- drop_safeframe(x)

# Replace the names of out that are in intersection with corresponding labels
names(out)[match(names(labels), names(out))] <- labels[names(labels)]
Expand Down
4 changes: 2 additions & 2 deletions R/lost_labels.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ lost_labels <- function(old, new, lost_action) {
)
if (lost_action == "warning") {
# nolint next: condition_call_linter.
warning(warningCondition(msg, class = "datatagr_warning"))
warning(warningCondition(msg, class = "safeframe_warning"))
}
if (lost_action == "error") {
# nolint next: condition_call_linter.
stop(errorCondition(msg, class = "datatagr_error"))
stop(errorCondition(msg, class = "safeframe_error"))
}
}
}
18 changes: 9 additions & 9 deletions R/lost_labels_action.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#' Check and set behaviour for lost labels
#'
#' This function determines the behaviour to adopt when labelled variables of a
#' `datatagr` are lost for example through subsetting. This is achieved using
#' `options` defined for the `datatagr` package.
#' `safeframe` are lost for example through subsetting. This is achieved using
#' `options` defined for the `safeframe` package.
#'
#' @param action a `character` indicating the behaviour to adopt when labelled
#' variables have been lost: "error" (default) will issue an error; "warning"
Expand All @@ -11,10 +11,10 @@
#' @param quiet a `logical` indicating if a message should be displayed; only
#' used outside pipelines
#'
#' @return returns `NULL`; the option itself is set in `options("datatagr")`
#' @return returns `NULL`; the option itself is set in `options("safeframe")`
#'
#' @details The errors or warnings generated by datatagr in case of labelled
#' variable loss has a custom class of `datatagr_error` and `datatagr_warning`
#' @details The errors or warnings generated by safeframe in case of labelled
#' variable loss has a custom class of `safeframe_error` and `safeframe_warning`
#' respectively.
#'
#' @export
Expand Down Expand Up @@ -43,11 +43,11 @@
#'
lost_labels_action <- function(action = c("warning", "error", "none"),
quiet = FALSE) {
datatagr_options <- options("datatagr")$datatagr
safeframe_options <- options("safeframe")$safeframe

action <- match.arg(action)
datatagr_options$lost_labels_action <- action
options(datatagr = datatagr_options)
safeframe_options$lost_labels_action <- action
options(safeframe = safeframe_options)
if (!quiet) {
if (action == "warning") msg <- "Lost labels will now issue a warning."
if (action == "error") msg <- "Lost labels will now issue an error."
Expand All @@ -64,5 +64,5 @@ lost_labels_action <- function(action = c("warning", "error", "none"),
#' @rdname lost_labels_action

get_lost_labels_action <- function() {
options("datatagr")$datatagr$lost_labels_action
options("safeframe")$safeframe$lost_labels_action
}
24 changes: 12 additions & 12 deletions R/make_datatagr.R → R/make_safeframe.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#' Create a datatagr from a data.frame
#' Create a safeframe from a data.frame
#'
#' This function converts a `data.frame` or a `tibble` into a `datatagr` object,
#' where data are labelled and validated. The output will seem to be the same
#' `data.frame`, but `datatagr`-aware packages will then be able to
#' This function converts a `data.frame` or a `tibble` into a `safeframe`
#' object, where data are labelled and validated. The output will seem to be the
#' same `data.frame`, but `safeframe`-aware packages will then be able to
#' automatically use labelled fields for further data cleaning and analysis.
#'
#' @param x a `data.frame` or a `tibble`
Expand All @@ -14,18 +14,18 @@
#'
#' @seealso
#'
#' * An overview of the [datatagr] package
#' * [labels()]: for a list of labelled variables in a `datatagr`
#' * An overview of the [safeframe] package
#' * [labels()]: for a list of labelled variables in a `safeframe`
#' * [set_labels()]: for modifying labels
#' * [labels_df()]: for selecting variables by labels
#'
#' @export
#'
#' @return The function returns a `datatagr` object.
#' @return The function returns a `safeframe` object.
#'
#' @examples
#'
#' x <- make_datatagr(cars,
#' x <- make_safeframe(cars,
#' speed = "Miles per hour",
#' dist = "Distance in miles"
#' )
Expand All @@ -41,13 +41,13 @@
#' speed = "Miles per hour",
#' dist = "Distance in miles"
#' )
#' new_x <- make_datatagr(cars, !!!my_labels)
#' new_x <- make_safeframe(cars, !!!my_labels)
#'
#' ## The output is strictly equivalent to the previous one
#' identical(x, new_x)
#'
make_datatagr <- function(x,
...) {
make_safeframe <- function(x,
...) {
# assert inputs
checkmate::assert_data_frame(x, min.cols = 1)
assert_not_data_table(x)
Expand All @@ -56,6 +56,6 @@ make_datatagr <- function(x,
x <- label_variables(x, labels)

# shape output and return object
class(x) <- c("datatagr", class(x))
class(x) <- c("safeframe", class(x))
x
}
17 changes: 9 additions & 8 deletions R/names.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#' Rename columns of a datatagr
#' Rename columns of a safeframe
#'
#' This function can be used to rename the columns a `datatagr` (that is, adjust
#' This function can be used to rename the columns a `safeframe` (that is,
#' adjust
#' variable names).
#'
#' @param x a `datatagr` object
#' @param x a `safeframe` object
#'
#' @param value a `character` vector to set the new names of the columns of `x`
#'
#' @return a `datatagr` with new column names
#' @return a `safeframe` with new column names
#'
#' @export
#'
#' @examples
#' ## create datatagr
#' x <- make_datatagr(cars,
#' ## create safeframe
#' x <- make_safeframe(cars,
#' speed = "Miles per hour",
#' dist = "Distance in miles"
#' )
Expand All @@ -33,7 +34,7 @@
#' head(x)
#' labels(x)
#' }
`names<-.datatagr` <- function(x, value) {
`names<-.safeframe` <- function(x, value) {
# Strategy for renaming

# Since renaming cannot drop columns, we can update labels to match new
Expand All @@ -42,7 +43,7 @@
# 1. Storing old names and new names to have define replacement rules
# 2. Replace all labelled variables using the replacement rules

out <- drop_datatagr(x)
out <- drop_safeframe(x)
names(out) <- value

# Step 1
Expand Down
18 changes: 9 additions & 9 deletions R/print.datatagr.R → R/print.safeframe.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#' Printing method for datatagr objects
#' Printing method for safeframe objects
#'
#' This function prints datatagr objects.
#' This function prints safeframe objects.
#'
#' @param x a `datatagr` object
#' @param x a `safeframe` object
#'
#' @param ... further arguments to be passed to 'print'
#'
Expand All @@ -11,8 +11,8 @@
#' @export
#'
#' @examples
#' ## create datatagr
#' x <- make_datatagr(cars,
#' ## create safeframe
#' x <- make_safeframe(cars,
#' speed = "Miles per hour",
#' dist = "Distance in miles"
#' )
Expand All @@ -24,14 +24,14 @@
#' if (require(tibble) && require(magrittr)) {
#' cars %>%
#' tibble() %>%
#' make_datatagr(
#' make_safeframe(
#' speed = "Miles per hour",
#' dist = "Distance in miles"
#' )
#' }
print.datatagr <- function(x, ...) {
cat("\n// datatagr object\n")
print(drop_datatagr(x))
print.safeframe <- function(x, ...) {
cat("\n// safeframe object\n")
print(drop_safeframe(x))

# Extract names and values from labels(x)
label_values <- unlist(labels(x))
Expand Down
Loading

0 comments on commit 63552c2

Please sign in to comment.