Skip to content

Commit

Permalink
Run style_pkg(), document()
Browse files Browse the repository at this point in the history
  • Loading branch information
chartgerink committed Sep 11, 2024
1 parent 20727eb commit 6bcd44b
Show file tree
Hide file tree
Showing 52 changed files with 550 additions and 550 deletions.
16 changes: 7 additions & 9 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ S3method("[<-",datatagr)
S3method("[[<-",datatagr)
S3method("names<-",datatagr)
S3method(print,datatagr)
export(get_lost_tags_action)
export(has_tag)
export(lost_tags_action)
export(get_lost_labels_action)
export(has_label)
export(labels)
export(labels_df)
export(lost_labels_action)
export(make_datatagr)
export(set_tags)
export(tags)
export(tags_df)
export(tags_types)
export(set_labels)
export(type)
export(validate_datatagr)
export(validate_tags)
export(validate_labels)
export(validate_types)
importFrom(lifecycle,deprecated)
importFrom(utils,modifyList)
12 changes: 6 additions & 6 deletions R/datatagr-package.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#' Base Tools for Labelling and Validating Data
#'
#' The *datatagr* package provides tools to help label and validate data. The
#' 'datatagr' class adds variable level attributes to 'data.frame' columns.
#' Once tagged, these variables can be seamlessly used in downstream analyses,
#' 'datatagr' class adds variable level attributes to 'data.frame' columns.
#' Once tagged, these variables can be seamlessly used in downstream analyses,
#' making data pipelines more robust and reliable.
#'
#' @aliases datatagr
Expand Down Expand Up @@ -40,9 +40,9 @@
#'
#' * `print()`: prints info about the `datatagr` in addition to the
#' `data.frame` or `tibble`
#'
#' @note The package does not aim to have complete integration with [dplyr]
#' functions. For example, [dplyr::mutate()] and [dplyr::bind_rows()] will
#'
#' @note The package does not aim to have complete integration with [dplyr]
#' functions. For example, [dplyr::mutate()] and [dplyr::bind_rows()] will
#' not preserve labels. We only provide compatibility for [dplyr::rename()].
#'
#' @examples
Expand Down Expand Up @@ -89,7 +89,7 @@
#' ) %>%
#' mutate(result = if_else(speed > 50, "fast", "slow")) %>%
#' set_labels(result = "Ticket yes/no")
#'
#'
#' head(x)
#'
#' ## extract labelled variables
Expand Down
2 changes: 1 addition & 1 deletion R/has_label.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#'
#' @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
#' maintain the variable labels at this time.
#'
Expand Down
24 changes: 13 additions & 11 deletions R/label_variables.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,34 @@
#'
#' @details If used several times, the previous label is removed silently.
#' Only accepts known variables from the provided `data.frame`.
#'
#'
label_variables <- function(x, labels) {
# Create an assertion collection to fill with assertions and potential errors
label_errors <- checkmate::makeAssertCollection()

# assert_choice() gives clearer error messages than assert_subset() so we
# use it in a loop with a assertion collection to ensure all issues are
# returned in the first run.
vapply(names(labels), FUN = function(namedLabel) {
checkmate::assert_choice(namedLabel, names(x),
null.ok = TRUE, add = label_errors)
checkmate::assert_choice(namedLabel, names(x),
null.ok = TRUE, add = label_errors
)
TRUE
}, FUN.VALUE = logical(1))

# Report back on the filled assertion collection
checkmate::reportAssertions(label_errors)

# Add the labels to the right location
# Vectorized approach does not work, so we use a for.. loop instead
for (name in names(labels)) {
label_value <- unlist(labels[names(labels) == name])

attr(x[[name]], "label") <- ifelse(is.null(label_value),
"",
as.character(label_value))

attr(x[[name]], "label") <- ifelse(is.null(label_value),
"",
as.character(label_value)
)
}

x
}
6 changes: 3 additions & 3 deletions R/labels.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#'
#' @export
#'
#' @return The function returns a named `list` where names indicate which column they correspond to, and values indicate
#' @return The function returns a named `list` where names indicate which column they correspond to, and values indicate
#' the relevant labels.
#'
#' @details Labels are stored as the `label` attribute of the column variable.
Expand All @@ -28,11 +28,11 @@
#' labels(x, TRUE)
labels <- function(x, show_null = FALSE) {
checkmate::assertClass(x, "datatagr")
out <- lapply(names(x), FUN = function (var) {
out <- lapply(names(x), FUN = function(var) {
attr(x[[var]], "label")
})
names(out) <- names(x)

# Filter out NULL values if show_null is FALSE
if (!show_null) {
out <- Filter(Negate(is.null), out)
Expand Down
4 changes: 2 additions & 2 deletions R/labels_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
#' labels_df(x)
labels_df <- function(x) {
checkmate::assertClass(x, "datatagr")

labels <- unlist(labels(x))
out <- drop_datatagr(x, remove_labels = TRUE)

# Find the intersection of names(out) and names(labels)
common_names <- intersect(names(out), names(labels))
# Replace the names of out that are in the intersection with the corresponding labels
names(out)[match(common_names, names(out))] <- labels[common_names]

out
}
10 changes: 5 additions & 5 deletions R/lost_labels.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ lost_labels <- function(old, new, lost_action) {

if (lost_action != "none" && length(lost_vars) > 0) {
lost_labels <- lapply(lost_vars, function(label) old[[label]])

lost_msg <- paste(lost_vars,
lost_labels,
sep = " - ",
collapse = ", "
lost_labels,
sep = " - ",
collapse = ", "
)
msg <- paste(
"The following labelled variables are lost:\n",
Expand All @@ -34,4 +34,4 @@ lost_labels <- function(old, new, lost_action) {
stop(errorCondition(msg, class = "datatagr_error"))
}
}
}
}
4 changes: 2 additions & 2 deletions R/lost_labels_action.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' 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
#' `datatagr` are lost for example through subsetting. This is achieved using
#' `options` defined for the `datatagr` package.
#'
#' @param action a `character` indicating the behaviour to adopt when labelled
Expand Down Expand Up @@ -42,7 +42,7 @@
#' lost_labels_action()
#'
lost_labels_action <- function(action = c("warning", "error", "none"),
quiet = FALSE) {
quiet = FALSE) {
datatagr_options <- options("datatagr")$datatagr

action <- match.arg(action)
Expand Down
2 changes: 1 addition & 1 deletion R/names.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@
names(out_labels) <- new_names
out <- label_variables(out, out_labels)
class(out) <- class(x)

out
}
2 changes: 1 addition & 1 deletion R/print.datatagr.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ print.datatagr <- function(x, ...) {
labels_txt <- "[no labelled variables]"
}
cat("\nlabels:", labels_txt, "\n")

invisible(x)
}
8 changes: 4 additions & 4 deletions R/remove_label.r
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#' Internal. Remove the "label" attribute from a specific variable in a data frame
#'
#'
#' @noRd
#'
#'
remove_label <- function(x, var) {
attr(x[[var]], "label") <- NULL
attr(x[[var]], "label") <- NULL
x
}
}
31 changes: 16 additions & 15 deletions R/restore_labels.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#' Restore labels of a datatagr
#'
#' Internal. This function is used to restore labels of a `datatagr` object which
#' may have lost its labels after handling for example through `dplyr` verbs.
#' Specific actions can be triggered when some of the labelled variables have
#' disappeared from the object.
#' may have lost its labels after handling for example through `dplyr` verbs.
#' Specific actions can be triggered when some of the labelled variables have
#' disappeared from the object.
#'
#' @param x a `data.frame`
#'
Expand All @@ -28,21 +28,22 @@ restore_labels <- function(x, newLabels,
checkmate::assertClass(x, "data.frame")
checkmate::assertClass(newLabels, "list")
lost_action <- match.arg(lost_action)

# Match the remaining variables to the provided labels
common_vars <- intersect(names(x), names(newLabels))
if (length(common_vars) == 0 && length(names(x) >0))
stop('No matching labels provided.')

if (length(common_vars) == 0 && length(names(x) > 0)) {
stop("No matching labels provided.")
}

lost_vars <- setdiff(names(newLabels), names(x))

if (lost_action != "none" && length(lost_vars) > 0) {
lost_labels <- lapply(lost_vars, function(label) newLabels[[label]])

lost_msg <- paste(lost_vars,
lost_labels,
sep = " - ",
collapse = ", "
lost_labels,
sep = " - ",
collapse = ", "
)
msg <- paste(
"The following labelled variables are lost:\n",
Expand All @@ -57,15 +58,15 @@ restore_labels <- function(x, newLabels,
stop(errorCondition(msg, class = "datatagr_error"))
}
}

for (name in common_vars) {
attr(x[[name]], "label") <- as.character(newLabels[[name]])
}

# Ensure class consistency
if (!inherits(x, "datatagr")) {
class(x) <- c("datatagr", class(x))
}

x
}
6 changes: 3 additions & 3 deletions R/set_labels.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Change labels of a datatagr object
#'
#' This function changes the `labels` of a `datatagr` object, using the same
#' syntax as the constructor [make_datatagr()].
#' syntax as the constructor [make_datatagr()].
#'
#' @inheritParams make_datatagr
#'
Expand Down Expand Up @@ -32,8 +32,8 @@
set_labels <- function(x, ...) {
# assert inputs
checkmate::assertClass(x, "datatagr")

labels <- rlang::list2(...)

label_variables(x, labels)
}
Loading

0 comments on commit 6bcd44b

Please sign in to comment.