REDCapCAST

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(REDCapCAST)

This vignette covers the basics to get you started with the two basic features of REDCapCAST:

Casting meta data

The easiest way is to use the shiny_cast(). You can access a hosted version here or launch it locally like this:

shiny_cast()

Reading data from REDCap

To get you started, the easiest way possible, you can use the easy_redcap() function (example below).

You will need an API-key for your REDCap server, the uri/URL/address for the API connection (usually the address used for accessing your institutions REDCap server, with an appended "/api/").

This function includes a few convenience features to ease your further work.

If your project uses repeating instruments possible as a longitudinal project, you can choose to widen the data. If not, the result will be a list of each instrument you have chosen to extract data from. Make sure to specify only the fields or instruments you need, and avoid to save any of the data locally, but always source from REDCap to avoid possibly insecure local storage of sensitive data.

easy_redcap(
  uri = "YOUR URI",
  project.name = "MY_PROJECT",
  widen.data = TRUE,
  fields = c("record_id", "OTHER FIELDS")
)

Splitting the dataset

The easy_redcap() function does a few things under the hood. Below are a few examples to show how the nicely formatted output is achieved.

A sample dataset and Data Dictionary/metadata is provided for this demonstration:

redcapcast_data |> gt::gt()
redcapcast_meta |> gt::gt()

To save the metadata as labels in the dataset, we can save field labels and the choices from radio buttons and dropdown features:

labelled_data <-
  apply_field_label(
    data = redcapcast_data,
    meta = redcapcast_meta
  ) |>
  apply_factor_labels(meta = redcapcast_meta)

The REDCap_split function splits the data set into a list of data.frames.

list <-
  REDCap_split(
    records = labelled_data,
    metadata = redcapcast_meta,
    forms = "all"
  ) |>
  # Next steps cleans up and removes generic columns
  sanitize_split()
str(list)

The easy_redcap() will then (optionally) continue to widen the data, by transforming the list of data.frames to a single data.frame with one row for each subject/record_id (wide data format):

wide_data <- redcap_wider(list,
  event.glue = "{.value}____{redcap_event_name}",
  inst.glue = "{.value}____{redcap_repeat_instance}"
)
wide_data |> str()

Transfer suffixes to labels:

wide_data_suffixes <- wide_data |> suffix2label()

Creating a nice table

wide_data_suffixes |> 
  as_factor()|>
  dplyr::select(sex, hypertension, diabetes,mrs_score____follow2) |>
  gtsummary::tbl_summary(type = gtsummary::all_dichotomous() ~ "categorical")


Try the REDCapCAST package in your browser

Any scripts or data that you put into this service are public.

REDCapCAST documentation built on April 4, 2025, 3:18 a.m.