rd_checkbox: Transform Checkbox Variables in a REDCap Project

View source: R/rd_checkbox.R

rd_checkboxR Documentation

Transform Checkbox Variables in a REDCap Project

Description

[Stable]

This function is used to process checkbox variables in a REDCap dataset. By default, it changes their default categories ("Unchecked" and "Checked") to new ones ("No" and "Yes). Optionally, the function can also evaluate the branching logic for checkbox fields and adjust the data and dictionary accordingly.

Usage

rd_checkbox(
  project = NULL,
  data = NULL,
  dic = NULL,
  event_form = NULL,
  checkbox_labels = c("No", "Yes"),
  checkbox_names = TRUE,
  na_logic = "missing"
)

Arguments

project

A list containing the REDCap data, dictionary, and event mapping (expected redcap_data() output). Overrides data, dic, and event_form.

data

A data.frame or tibble with the REDCap dataset.

dic

A data.frame with the REDCap dictionary.

event_form

Only applicable for longitudinal projects (presence of events). Event-to-form mapping for longitudinal projects.

checkbox_labels

Character vector of length 2 for labels of unchecked/checked values. Default: c("No", "Yes").

checkbox_names

Logical. If TRUE (default), checkbox columns are renamed using choice labels. With FALSE, the columns keep their names and each option is a field of the dictionary: running the function again on its result stops with an error instead of converting them again. Without the .factor versions of the options, the values of the columns become the labels; with them (as in the data of redcap_data()), the columns keep their codes 0/1 and their .factor versions take the labels.

na_logic

Controls how missing values are set based on branching logic. Must be one of "none" (do nothing), "missing" (set to NA only when the logic evaluation is NA), or "eval" (set to NA when the logic evaluates to FALSE or NA). Defaults to "missing".

Details

  • Checkbox columns are expected in REDCap wide format (field___code).

  • Branching logic evaluation requires event_form for longitudinal projects.

  • Names are cleaned and truncated to 60 characters; uniqueness is enforced.

  • Fields that cannot be evaluated are listed in results.

  • In the branching logic and calculations, the options get their new names and keep their codes 0 and 1, as in REDCap logic (e.g. ⁠[cb(1)] = '1'⁠ is ⁠[cb_option] = '1'⁠, and ⁠[cb(1)] + [cb(2)] >= 2⁠ is ⁠[cb_option] + [cb_other] >= 2⁠). rd_dictionary() writes the labels when it translates the logic into R.

Value

A list with:

data

Transformed dataset with the checkbox options optionally renamed. When the data has the .factor versions of the options, the original columns keep their codes 0/1 and the .factor versions take the labels of checkbox_labels; otherwise the original columns are replaced by factors with these labels.

dictionary

Updated dictionary with checkbox fields expanded and optionally renamed.

event_form

The event_form passed in (if applicable).

results

Summary of transformations and any fields needing review.

See Also

Other process functions: rd_dates(), rd_delete_vars(), rd_dictionary(), rd_factor(), rd_insert_na(), rd_recalculate(), rd_rlogic(), rd_split(), rd_transform()

Examples

# Basic usage with a project object
res <- rd_checkbox(covican)

# With custom labels
res <- rd_checkbox(data = covican$data,
                   dic = covican$dictionary,
                   event_form = covican$event_form,
                   checkbox_labels = c("Not present", "Present"))

# Keep original checkbox names
res <- rd_checkbox(covican, checkbox_names = FALSE)

# Longitudinal project with NA logic
res <- rd_checkbox(data = covican$data,
                   dic = covican$dictionary,
                   event_form = covican$event_form,
                   na_logic = "eval")


REDCapDM documentation built on Nov. 27, 2026, 9:06 a.m.