CHOP-CGTInformatics / REDCapTidieR

Makes it easy to read REDCap Projects into R
https://chop-cgtinformatics.github.io/REDCapTidieR/
Other
33 stars 8 forks source link

[FEATURE] an option `raw_or_label = "haven_labelled"` for `read_redcap()` #178

Closed larmarange closed 8 months ago

larmarange commented 8 months ago

Feature Request Description

For some projects, our data workflow requires to import data from REDCap, make diverse transformations within R and then re-export data to several formats including Stata and SPSS using haven package.

For categorical variables, we need to keep the original codes used to collect data in REDCap while providing value labels. In haven, such data format is handled with the haven_labelled class, and several packages exist to deal with it, in particular the labelled package: https://larmarange.github.io/labelled/ (to be noted, this package is already suggested by REDCapTidieR).

For this purpose, it would be great to be able to import datasets from REDCap directly with the haven_labelled format, allowing to store both codes and value labels in the dataset.

Proposed Solution

For that, it would be a great addition to have an option raw_or_label = "haven_labelled" in read_redcap() or alternatively a function make_haven_labelled() that could be applied on a super-tibble.

Technically:

  1. data would have to be imported as "raw" with the current implementation of read_redcap()
  2. the column select_choices_or_calculations in redcap_metadata will have to be parsed to identify and generate a list of value labels
  3. then, such list could be easily applied to each tibble using labelled::set_value_labels() (through .labels argument)

In fact, the real piece of work is the point 2.

Describe alternatives you've considered

We are considering developing our own internal function. However, we have the feeling that such functionality could be useful for others and that it would be relevant to be integrated in an existing package.

Additional Context

Please let us know if you think that such functionality would be relevant for REDCapTidieR. If yes, we would be happy to contribute and/or propose a PR.

Checklist

larmarange commented 8 months ago

Exploring the code of your package, it seems that most of the work is already done in the internal function multi_choice_to_labels(). It would therefore be releatively easy to have an option in this function to convert into an haven_labelled vector instead of a factor.

ezraporter commented 8 months ago

Thanks for the feature request! This sounds generally useful so we plan on exploring this week and seeing what we can add. We'll be in touch soon!

larmarange commented 8 months ago

Thank you.

ezraporter commented 8 months ago

See if #180 addresses your request. We're definitely interested in your feedback!

This edge case may be of general interest to you too: https://github.com/CHOP-CGTInformatics/REDCapTidieR/pull/180#discussion_r1526626425

larmarange commented 8 months ago

I just tested. It worked perfectly. Thanks a lot.