Skip to content

Commit

Permalink
Fix lintr issues
Browse files Browse the repository at this point in the history
  • Loading branch information
chartgerink committed Sep 11, 2024
1 parent 5bc2f1e commit 4044edd
Show file tree
Hide file tree
Showing 21 changed files with 65 additions and 55 deletions.
4 changes: 2 additions & 2 deletions R/datatagr-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
#' variables are lost (e.g. removing columns storing labelled variables) to
#' issue warnings, errors, or do nothing
#'
#' * [get_lost_labels_action()]: to check the current behaviour of actions where
#' labelled variables are lost
#' * [get_lost_labels_action()]: to check the current behaviour of actions
#' where labelled variables are lost
#'
#' @section Dedicated methods:
#'
Expand Down
4 changes: 2 additions & 2 deletions R/drop_datatagr.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#'
#' @param x a `datatagr` object
#'
#' @param remove_labels a `logical` indicating if labels should be removed from the
#' attributes; defaults to `TRUE`
#' @param remove_labels a `logical` indicating if labels should be removed from
#' the attributes; defaults to `TRUE`
#'
#' @noRd
#'
Expand Down
6 changes: 4 additions & 2 deletions R/label_variables.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#' Add labels to variables
#'
#' Internal. This function will label pre-defined variables in a
#' `data.frame` by adding a label attribute to the column. This can be used for one or multiple variables at the same time.
#' `data.frame` by adding a label attribute to the column. This can be used for
#' one or multiple variables at the same time.
#'
#' @param x a `data.frame` or a `tibble`, with at least one column
#'
#' @param labels A named list with variable names in `x` as list names and the labels as list values. Values set to `NULL` remove the label.
#' @param labels A named list with variable names in `x` as list names and the
#' labels as list values. Values set to `NULL` remove the label.
#'
#' @noRd
#'
Expand Down
8 changes: 4 additions & 4 deletions R/labels.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Get the list of labels in a datatagr
#'
#' This function returns the list of labels identifying specific variable types in
#' a `datatagr`.
#' This function returns the list of labels identifying specific variable types
#' in a `datatagr` object.
#'
#' @param x a `datatagr` object
#'
Expand All @@ -11,8 +11,8 @@
#'
#' @export
#'
#' @return The function returns a named `list` where names indicate which column they correspond to, and values indicate
#' the relevant labels.
#' @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 Down
10 changes: 5 additions & 5 deletions R/labels_df.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#' 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.
#' 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.
#'
#' @param x a `datatagr` object
#'
Expand All @@ -23,11 +23,11 @@ labels_df <- function(x) {
checkmate::assertClass(x, "datatagr")

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

# 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
# Replace the names of out that are in intersection with corresponding labels
names(out)[match(common_names, names(out))] <- labels[common_names]

out
Expand Down
12 changes: 7 additions & 5 deletions R/make_datatagr.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#' Create a datatagr 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 automatically use labelled
#' fields for further data cleaning and analysis.
#' 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
#' automatically use labelled fields for further data cleaning and analysis.
#'
#' @param x a `data.frame` or a `tibble`
#'
#' @param ... <[`dynamic-dots`][rlang::dyn-dots]> A named list with variable names in `x` as list names and the labels as list values. Values set to `NULL` remove the label. When specifying labels, please also see
#' `default_values`.
#' @param ... <[`dynamic-dots`][rlang::dyn-dots]> A named list with variable
#' names in `x` as list names and the labels as list values. Values set to
#' `NULL` remove the label. When specifying labels, please also see
#' `default_values`.
#'
#' @seealso
#'
Expand Down
6 changes: 3 additions & 3 deletions R/names.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
`names<-.datatagr` <- function(x, value) {
# Strategy for renaming

# Since renaming cannot drop columns, we can update labels to match new variable
# names. We do this by:
# Since renaming cannot drop columns, we can update labels to match new
# variable names. We do this by:

# 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, remove_labels = TRUE)
out <- drop_datatagr(x)
names(out) <- value

# Step 1
Expand Down
2 changes: 1 addition & 1 deletion R/print.datatagr.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#' }
print.datatagr <- function(x, ...) {
cat("\n// datatagr object\n")
print(drop_datatagr(x, remove_labels = TRUE))
print(drop_datatagr(x))

# Extract names and values from labels(x)
label_values <- unlist(labels(x))
Expand Down
2 changes: 1 addition & 1 deletion R/remove_label.r
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Internal. Remove the "label" attribute from a specific variable in a data frame
#' Internal. Remove "label" attribute from a specific variable in a data frame
#'
#' @noRd
#'
Expand Down
16 changes: 8 additions & 8 deletions R/restore_labels.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#' 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.
#' 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.
#'
#' @param x a `data.frame`
#'
#' @param labels a list of labels as returned by [labels()]; if default values are
#' missing, they will be added to the new list of labels. Matches column names
#' with `x` to restore labels. Throws an error if no matches are found.
#' @param labels a list of labels as returned by [labels()]; if default values
#' are missing, they will be added to the new list of labels. Matches column
#' names with `x` to restore labels. Throws an error if no matches are found.
#'
#' @param lost_action a `character` indicating the behaviour to adopt when
#' labelled variables have been lost: "error" (default) will issue an error;
Expand All @@ -31,7 +31,7 @@ restore_labels <- function(x, newLabels,

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

Expand Down
4 changes: 1 addition & 3 deletions R/square_bracket.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
#' set_labels(result = "Ticket")
#' x
#'
#' ## dangerous removal of a labelled column setting it to NULL issues a warning
#' ## dangerous removal of a labelled column setting it to NULL issues warning
#' x[, 1] <- NULL
#' x
#'
Expand Down Expand Up @@ -137,7 +137,6 @@
old_labels <- labels(x, show_null = TRUE)
new_labels <- old_labels

# new_labels[[i]] <- attr(value, "label")
# Check if the assignment is to the "label" attribute
if (missing(j) && !is.null(attr(value, "label"))) {
new_labels[[i]] <- attr(value, "label")
Expand All @@ -163,7 +162,6 @@
old_labels <- labels(x, show_null = TRUE)
new_labels <- old_labels

# new_labels[[name]] <- attr(value, "label")
# Check if the assignment is to the "label" attribute
if (is.null(attr(x[[name]], "label")) && !is.null(attr(value, "label"))) {
new_labels[[name]] <- attr(value, "label")
Expand Down
3 changes: 2 additions & 1 deletion R/validate_types.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
#'
#' @param x a `datatagr` object
#'
#' @param ... <[`dynamic-dots`][rlang::dyn-dots]> A named list with variable names in `x` as list names and the related types as list values.
#' @param ... <[`dynamic-dots`][rlang::dyn-dots]> A named list with variable
#' names in `x` as list names and the related types as list values.
#'
#' @return A named `list`.
#'
Expand Down
3 changes: 2 additions & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.onLoad <- function(libname, pkgname) {
lost_labels_action(Sys.getenv("DATATAGR_LOST_ACTION", "warning"), quiet = TRUE)
lost_labels_action(Sys.getenv("DATATAGR_LOST_ACTION", "warning"),
quiet = TRUE)
}
4 changes: 2 additions & 2 deletions man/datatagr-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions man/labels.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions man/labels_df.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions man/make_datatagr.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/set_labels.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/sub_datatagr.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/validate_datatagr.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/validate_types.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4044edd

Please sign in to comment.