castoredc / castoRedc

R wrapper to Castor EDC API
MIT License
3 stars 8 forks source link

Analyzing Factors with Optiongroups #10

Closed reiniervlinschoten closed 7 months ago

reiniervlinschoten commented 3 years ago

Hi,

I am using the following code to retrieve all study data:

castor_api$getStudyData("STUDY_ID",
                                     report_instances = TRUE,
                                     survey_instances = TRUE) 
surveydata <- attr(studydata, "survey_instances")
reportdata <- attr(studydata, "report_instances")

But this retrieves all the categorical variables as numeric (it returns their option values and not the corresponding option). Is there a flag that can be used to automatically apply the option groups to their respective variables, or do I have to code that myself?

Sjoerd-dev commented 2 years ago

Has this issue been fixed for you?

reiniervlinschoten commented 2 years ago

No, I have been using the Python wrapper which also works in R: https://github.com/reiniervlinschoten/castoredc_api-R

Op ma 21 feb. 2022 17:52 schreef Sjoerd @.***>:

Has this issue been fixed for you?

— Reply to this email directly, view it on GitHub https://github.com/castoredc/castoRedc/issues/10#issuecomment-1047279607, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACSQFLLSEU7QQKRMBHSM7VLU4K635ANCNFSM4VG3FCEQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you authored the thread.Message ID: @.***>

Sjoerd-dev commented 2 years ago

Okay. For anybody who need to convert radiobutton or dropdown fields from Castor-optiongroup-value to Castor-optiongroup-label here is a script I use in R to convert a set of fields:

 # Convert dropdown options to labels 

  # Select variable names to retrieve CastorEDC OptionGroups from
  # Use: create DF with column "variable.name" and column "output.name"
  # the exact variable names will be looked up in the CastorEDC metadata
  # and saved as individual DFs with the defined names
  # Example: 
  # Options.lookup    <-  tibble(variable.name = c("int_pt_sports"),
  #                                                output.name = c("sports.options"))
  options.lookup    <-  tibble(
    variable.name = c("int_pt_sports", "ecg_concl_crit", "ecg_axis_r_fac", "ecg_meas_p_enl_LorR", "ecg_qrs_volt", "covid19_compl_severity"),
    output.name = c("sports.options", "ecg.concl.crit", "ecg.axis.factor", "ecg.atria.factor", "ecg.qrsvolt.factor", "covid19_sev_factor")
  )               # Todo: automation based on field type (i.e. radiobutton/dropdown)

  # This part looks up OptionGroups used in Castor database.
  # It finds a variable name in the metadata (metadata <- castor_api$generateFieldMetadata(InterestingStudy)) and looks up the details of the corresponding option group
  if (exists('options.lookup')
      && !(is.null(dim(options.lookup)))
      && is.data.frame(get('options.lookup'))) {
    for (i in 1:nrow(options.lookup)) {
      # Save conversiontable for each interesting option group (defined in df 'options.lookup')
      options_metadata <-
        # Look-up field metadata (including option groups)
        castor_api$getField(
          study_id = InterestingStudy,
          include = "optiongroup",
          # Use the 'field_id' found in the metadata of all variables
          field_id = metadata[metadata$field_variable_name %in%
                                options.lookup$variable.name[i],] %>%
            .$field_id
        )

      # Save results in pre-defined dataframes
      options_metadata <-
        tibble(
          "value" = c(options_metadata[["option_group"]][["options"]][["value"]]),
          "name" = c(options_metadata[["option_group"]][["options"]][["name"]])
        )
      assign(options.lookup$output.name[i],
             options_metadata)
    }
    # Replace values with names in all_data, if DF sports.options exists

    for (i in 1:nrow(options.lookup)) {
      var <- options.lookup$variable.name[i]
      lab <- options.lookup$output.name[i]

      if (exists(lab)  &&
          !(is.null(dim(get(lab)))) &&
          is.data.frame(get(lab))) {
        if (var %in% colnames(studydata)) {
          studydata[[var]] <-
            studydata[[var]] %>%
            factor(levels = get(lab)$value,
                   labels = get(lab)$name)
        }

        if (var %in% colnames(reportdata)) {
          reportdata[[var]] <-
            reportdata[[var]] %>%
            factor(levels = get(lab)$value,
                   labels = get(lab)$name)
        }

      }
    }

    rm(list = options.lookup$output.name)
    rm(options.lookup, var, lab)
  }
  rm(options_metadata, i)

I hope to learn how to work with GitHub in the near future so I can help contribute to the general code.

slknijnenburg commented 7 months ago

Fixed with PR #38