Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: tidyREDCap
Title: Helper Functions for Working with 'REDCap' Data
Version: 1.2.0
Version: 2.0.0
Authors@R:
c(person(
given = "Raymond",
Expand Down Expand Up @@ -72,6 +72,7 @@ Imports:
dplyr,
janitor,
labelVector,
lifecycle,
magrittr,
purrr,
REDCapR,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ importFrom(janitor,adorn_pct_formatting)
importFrom(janitor,tabyl)
importFrom(labelVector,is_labelled)
importFrom(labelVector,set_label)
importFrom(lifecycle,deprecate_soft)
importFrom(magrittr,"%>%")
importFrom(purrr,map_chr)
importFrom(purrr,map_df)
Expand Down
14 changes: 14 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# tidyREDCap 2.0.0 (CRAN release)

## Breaking changes

* `drop_label()` now preserves variable class attributes (e.g., `"character"`, `"numeric"`) while removing only the `"labelled"` class and `label` attribute. Previous versions removed all attributes.

## New features

* `drop_label()` now can be used on data frames. When called without arguments on a data frame, it removes labels from all variables.

## Fixes/Changes

* `drop_labels()` is now deprecated in favor of `drop_label()`. Use `drop_label()` without arguments to remove labels from all variables.

# tidyREDCap 1.2.0 (CRAN release)

* Updated `drop_label()` now accepts multiple variables and uses `tidyselect` helpers.
Expand Down
84 changes: 84 additions & 0 deletions R/drop_label.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@

#' Drop attributes/labels from variables or data frames
#'
#' @description Some functions don't work with labelled variables. As a solution,
#' this function can be used to drop labels (and all other attributes) from
#' one or more variables within a data frame, from all variables if none are
#' specified, or from a vector directly.
#'
#' @param x A data frame or a vector/column.
#' @param ... When `x` is a data frame, select variables using tidyselect
#' helpers (e.g., `contains()`, `starts_with()`) or unquoted names.
#' If empty, removes labels from ALL variables. Ignored when `x` is a vector.
#'
#' @return The modified data frame or vector with attributes removed.
#'
#' @examples
#' \dontrun{
#' # Dataset-level: Remove labels from ALL variables
#' df |> drop_label()
#'
#' # Dataset-level: Remove labels from specific variables
#' df |> drop_label(employment, starts_with("dem_"))
#'
#' # Variable-level: Use inside mutate
#' df |> mutate(name_first = drop_label(name_first))
#'
#' # Variable-level: Use with across()
#' df |> mutate(across(c(age, income), drop_label))
#' }
#'
#' @export
drop_label <- function(x, ...) {
# 1. Dataset-level logic
if (is.data.frame(x)) {
old_class_char <- class(x)

# Check if any variables were selected
vars_idx <- tidyselect::eval_select(rlang::expr(c(...)), x)

# If no variables selected, process ALL columns
if (length(vars_idx) == 0) {
# seq_along will either capture the variable column number
vars_idx <- seq_along(x)
}

# Remove attributes from selected columns
for (col_idx in vars_idx) {
# col_idx evaluates to:
# 1. variable name(s) if passed with the `...` argument
# 2. variable column position number if the whole dataset was passed for dropping

# Get all current attributes
col_attrs <- attributes(x[[col_idx]])

# Remove the label attribute
col_attrs$label <- NULL

# Remove the "labelled" from class if it exists
if (!is.null(col_attrs$class)) {
col_attrs$class <- col_attrs$class[col_attrs$class != "labelled"]
# If the class now becomes empty, remove it to use R's default
if (length(col_attrs$class) == 0) col_attrs$class <- NULL
}

# Reapply the modified attributes
attributes(x[[col_idx]]) <- col_attrs
}

# Preserve original class structure (data.frame vs tibble)
if (("data.frame" %in% old_class_char) && !("tbl_df" %in% old_class_char)) {
class(x) <- "data.frame"
}

return(x)
}

# 2. Variable-level logic
if (is.character(substitute(x))) {
stop('It looks like you quoted your variable. The variable must be unquoted when used inside mutate().')
}

attributes(x) <- NULL
return(x)
}
8 changes: 8 additions & 0 deletions R/drop_labels.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#' @param df The data frame with column labels that you want to drop
#'
#' @importFrom purrr map_df
#' @importFrom lifecycle deprecate_soft
#'
#' @export
#'
Expand All @@ -17,6 +18,13 @@
#' skimr::skim()
#' }
drop_labels <- function(df) {
# Add deprecate message:
lifecycle::deprecate_soft(
when = "1.2.0", # Version when this was deprecated
what = "drop_labels()",
with = "drop_label()"
)

old_class_char <- class(df)
if (!("data.frame" %in% old_class_char)) {
stop("df must have class data.frame", call. = FALSE)
Expand Down
48 changes: 0 additions & 48 deletions R/make_instrument_auto.R
Original file line number Diff line number Diff line change
Expand Up @@ -171,51 +171,3 @@ fix_class_bug <- function(df) {
return(df)
}
"fix_class_bug"


#' Drop attributes/labels from variables or data frames
#'
#' @description Some functions don't work with labelled variables. As a solution,
#' this function can be used to drop labels (and all other attributes) from
#' one or more variables within a data frame, or from a vector directly.
#'
#' @param x A data frame or a vector/column.
#' @param ... When `x` is a data frame, select variables using tidyselect
#' helpers (e.g., `contains()`, `starts_with()`) or unquoted names.
#' Ignored when `x` is a vector.
#'
#' @return The modified data frame or vector with attributes removed.
#'
#' @examples
#' \dontrun{
#' # Dataset-level: Remove labels from specific variables
#' df |> drop_label(employment, starts_with("dem_"))
#'
#' # Variable-level: Use inside mutate
#' df |> mutate(name_first = drop_label(name_first))
#'
#' # Variable-level: Use with across()
#' df |> mutate(across(c(age, income), drop_label))
#' }
#'
#' @export
drop_label <- function(x, ...) {
# 1. Dataset-level logic
if (is.data.frame(x)) {
vars_idx <- tidyselect::eval_select(rlang::expr(c(...)), x)
if (length(vars_idx) == 0) return(x)

for (col_idx in vars_idx) {
attributes(x[[col_idx]]) <- NULL
}
return(x)
}

# 2. Variable-level logic
if (is.character(substitute(x))) {
stop('It looks like you quoted your variable. The variable must be unquoted when used inside mutate().')
}

attributes(x) <- NULL
return(x)
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ tidyREDCap is an R package with functions for processing REDCap data.

* After loading data into R using RStudio with the `import_instruments()` function, you can see both the variable name and the text that appears to users of REDCap. All you need to do is click on the dataset's name in the **Environment** tab or use the `View()` function. The column headings will include both the Variable Name and the Field Label from REDCap.

* 💥 NEW in Version 1.1 💥 Functions coming from packages outside of `tidyREDCap` may not understand what to do with labeled variables. So, `tidyREDCap` includes a new `drop_labels()` function that will allow you to strip the labels before using functions that want unlabeled data.
* 💥 NEW in Version 2.0.0 💥 Functions coming from packages outside of `tidyREDCap` may not understand what to do with labeled variables. So, `tidyREDCap` includes a new `drop_label()` function that will allow you to strip the labels before using functions that want unlabeled data.

#### Working with <i>Choose One</i> Questions

Expand Down
10 changes: 7 additions & 3 deletions man/drop_label.Rd

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

Loading