OuhscBbmc / REDCapR

R utilities for interacting with REDCap
https://ouhscbbmc.github.io/REDCapR
Other
112 stars 45 forks source link

Looping through `checkbox_choices` function #514

Closed zkasper4life closed 7 months ago

zkasper4life commented 7 months ago

I am trying to re-create the data dictionary by modifying all of the checkbox (and other piped value) fields. The checkbox_choices function works perfectly when I try to perform on a single value in the dictionary:

checkbox_choices(dict$field_value[1])

> checkbox_choices(dict$field_value[1])
  id                label
1  1                 Black or African American
2  2                                     White
3  3                                     Asian
4  4          American Indian or Alaska Native
5  5 Native Hawaiian or Other Pacific Islander
6  6                               Multiracial
7  7                      Prefer not to answer

But when I try and apply this to all values in the field_value column, I am yelled at:

for(i in 1:length(dict$field_value)){
 checkbox_choices(dict$field_value[i]) %>% 
    rename('value'='id')
}
Error in checkbox_choices(dict$field_value[i]) : 
  Assertion on 'select_choices' failed: Contains missing values (element 1).

Any advice? For the record, the column field_value contains hundreds of values, so I'd very much like looping through the function compared to the alternative.

zkasper4life commented 7 months ago

I decided to take a more indirect route; required a few more steps, but still gave the results I anticipated:

dict %>% 
  mutate(field_value = strsplit(select_choices_or_calculations, "\\s\\|\\s")) %>% 
  unnest(field_value) %>% 
  separate(field_value, c("field_value","value_label"), sep = ",\\s", remove = F)