--- title: "Make a 'Choose One' Table" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Make a 'Choose One' Table} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#" ) ``` ```{r setup} library(tidyREDCap) ```
# The Problem It is often desirable to print variable labels above a summary table that shows the count of factor labels. The labels exported on _choose all that apply_ questions, including the question and whichever response was chosen. This redundancy is often unwanted, and the results are not presented professionally. For example, in the Nacho Craving Index data, the first ingredient is "Chips". We see how R presents this information by simply printing the components of the `ingredients___1` column. ```{r} redcap <- readRDS(file = "./redcap.rds") redcap$ingredients___1 ``` As we can see, this information is quite ugly, so we want to tabulate the results instead. However, if we use the simple `table()` function to clean up this information, we lose the original question and the answer label for `ingredients___1`. ```{r} table(redcap$ingredients___1) ``` We no longer know what the question was or which "select all" option this information represents. ## Aside: Loading REDCap Data into R See the [Import All Instruments from a REDCap Project](../doc/importInstruments.html) and [Importing from REDCap](../doc/useAPI.html) vignettes for details/information. # The Solution The `make_choose_one_table()` function can be used with a factor variable to tabulate the response *while preserving the question and checked option context*. ```{r example_raw} make_choose_one_table(redcap$ingredients___1) ``` Further, this output can be molded into a publication-ready table with a single additional function call. ```{r example_pretty, results="asis"} make_choose_one_table(redcap$ingredients___1) %>% knitr::kable() ``` The `subset` option, if set to `TRUE,` will cause the function to remove the label's text and only show the response option (i.e., not repeat the "What ingredients do you currently crave?" question). ```{r example2, results="asis", } make_choose_one_table( redcap$ingredients___2, subset = TRUE ) %>% knitr::kable() ``` This function can also be used in an analysis pipeline with a data frame name and the name of the factor inside that data frame. For example: ```{r example3, results="asis", fig.align="left"} redcap %>% make_choose_one_table(ingredients___3) %>% knitr::kable() ```