Make a ‘Choose All’ Table

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()
#>   ingredients___1 ingredients___2 ingredients___3 ingredients___4
#> 1         Checked         Checked         Checked         Checked
#> 2         Checked         Checked       Unchecked         Checked
#> 3       Unchecked       Unchecked       Unchecked       Unchecked
#> 4       Unchecked       Unchecked       Unchecked       Unchecked
#> 5       Unchecked       Unchecked       Unchecked       Unchecked
#> 6       Unchecked       Unchecked       Unchecked       Unchecked
#>   ingredients___5 ingredients___6 ingredients___7 ingredients___8
#> 1         Checked         Checked       Unchecked         Checked
#> 2         Checked       Unchecked         Checked         Checked
#> 3       Unchecked       Unchecked       Unchecked       Unchecked
#> 4       Unchecked       Unchecked       Unchecked       Unchecked
#> 5       Unchecked       Unchecked       Unchecked       Unchecked
#> 6       Unchecked       Unchecked       Unchecked       Unchecked

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") 
#> # A tibble: 8 x 2
#>   What          Count
#>   <chr>         <dbl>
#> 1 Chips             9
#> 2 Yellow cheese     7
#> 3 Orange cheese     3
#> 4 White cheese      4
#> 5 Meat              5
#> 6 Beans             7
#> 7 Tomatoes          6
#> 8 Peppers           8

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()
What Count
Chips 9
Yellow cheese 7
Orange cheese 3
White cheese 4
Meat 5
Beans 7
Tomatoes 6
Peppers 8