Make a 'Choose All' Table

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


The Problem

REDCap exports a "choose all that apply" question into a series of similarly-named, binary indicator variables (i.e., the variables are equal to either "checked" or "unchecked"). For example, the following data represents a sample of responses to the Nacho Craving Index.

redcap <- readRDS(file = "./redcap.rds")
redcap %>% 
  select(starts_with("ingredients___")) %>% 
  head()

It is desirable to have a consise table showing how often each option was chosen.

Aside: Loading REDCap Data into R

The redcapAPI package can be used to load data directly into R. To learn more about it, take a look here. Normally the code to automatically pull data with an API includes a person's secret code "key". Because I want to keep this hidden, I have hidden this API key in my user profile and the code below includes a call to Sys.getenv() to grab the key. To learn more about working with APIs, look here. Also notice that the data is saved using the saveRDS() function. REDCap data loaded with the API has the variable labels added as an extra attribute. To allow this vignette to run without sharing my secret key, I have saved the data to the package website.

rcon <- redcapAPI::redcapConnection(
  url = 'https://redcap.miami.edu/api/', 
  token = Sys.getenv("NCI_API_Key")
)

redcap <- redcapAPI::exportRecords(rcon)



The Solution

If you pass the make_choose_all_table() function the name of a REDCap export and the name of the choose all that apply question question in REDCap, it will produce a consise frequency count table.

make_choose_all_table(redcap, "ingredients") 

Similar to the make_choose_one_table() function, we can use this function inside an analysis pipeline. We can add the kable() call to make the table publication quality as well.

redcap %>% 
  make_choose_all_table("ingredients") %>% 
  knitr::kable()


Try the tidyREDCap package in your browser

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

tidyREDCap documentation built on Feb. 11, 2020, 1:06 a.m.