Skip to content

Commit

Permalink
Merge pull request #472 from pwildenhain/fix-redcap-coltypes-for-dags
Browse files Browse the repository at this point in the history
🐛 Fix coltypes for projects with dags
  • Loading branch information
wibeasley authored Apr 18, 2023
2 parents 6d3b771 + 60d8ff9 commit f6d9d00
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ This will help extract forms from longitudinal & repeating projects.
* `redcap_dag_read()` has new `data_access_group_id` field (introduced maybe in [13.1.0](https://community.projectredcap.org/articles/13/index.html)) (#459)
* `redcap_users_export()` has new `mycap_participants` field (introduced maybe in [13.0.0](https://community.projectredcap.org/articles/13/index.html)) (#459)
* Accommodate older versions of REDCap that don't return project-level variable, like `has_repeating_instruments_or_events`, `missing_data_codes`, `external_modules`, `bypass_branching_erase_field_prompt` (@the-mad-statter, #465, #466)
* `redcap_meta_coltypes()` correctly determines data type for autonumber `record_id` fields. It suggests a character if the project has DAGs, and an integer if not. (@pwildenhain, #472)


Version 1.1.0 (released 2022-08-10)
Expand Down
9 changes: 7 additions & 2 deletions R/redcap-metadata-coltypes.R
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,14 @@ redcap_metadata_internal <- function(
d_meta <- REDCapR::redcap_metadata_read( redcap_uri, token, verbose = verbose, handle_httr = handle_httr)$data
d_inst <- REDCapR::redcap_instruments( redcap_uri, token, verbose = verbose, handle_httr = handle_httr)$data
d_proj <- REDCapR::redcap_project_info_read(redcap_uri, token, verbose = verbose, handle_httr = handle_httr)$data
d_dags <- REDCapR::redcap_dag_read( redcap_uri, token, verbose = verbose, handle_httr = handle_httr)

# Determine status of autonumbering, instrument complete status, and decimal mark
.record_field <- d_var$original_field_name[1] # The first field should always be the "record" identifier.
.autonumber <- d_proj$record_autonumbering_enabled[1]
.plumbing_possibles <- c(.record_field, "redcap_event_name", "redcap_repeat_instrument", "redcap_repeat_instance")
# If the dags call fails, since the user is assigned to a DAG, then we assign .dags a value of TRUE
.dags <- nrow(d_dags$data) > 0 | grep("do not have permission", d_dags$raw_text)
.plumbing_possibles <- c(.record_field, "redcap_event_name", "redcap_repeat_instrument", "redcap_repeat_instance")
decimal_period <- (locale$decimal_mark == ".")
decimal_comma <- (locale$decimal_mark == ",")

Expand Down Expand Up @@ -371,12 +374,14 @@ redcap_metadata_internal <- function(
d <-
d_meta %>%
dplyr::mutate(
dags = (.dags & (.data$field_name == .record_field)),
autonumber = (.autonumber & (.data$field_name == .record_field)),
) %>%
dplyr::mutate(
response =
dplyr::case_when(
autonumber ~ paste0("col_integer()" , "~~record_autonumbering is enabled for the project"),
dags ~ paste0("col_character()" , "~~DAGs are enabled for the project"),
autonumber & !dags ~ paste0("col_integer()" , "~~record_autonumbering is enabled and DAGs are disabled for the project"),
field_type == "event_name" ~ paste0("col_character()" , "~~longitudinal event_name"),
field_type == "repeat_instrument" ~ paste0("col_character()" , "~~repeat_instrument"),
field_type == "repeat_instance" ~ paste0("col_integer()" , "~~repeat_instance"),
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-metadata-coltypes.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ test_that("simple", {

expect_equal(actual, expected=expected, label="The returned col_types should be correct", ignore_attr = TRUE) # dput(returned_object$data)
expect_s3_class(actual, "col_spec")
# Project has dags, so record_id should be a character
expect_equal(actual$cols$record_id, readr::col_character())

ds <-
redcap_read_oneshot(
Expand Down Expand Up @@ -68,6 +70,8 @@ test_that("longitudinal", {

expect_equal(actual, expected=expected, label="The returned col_types should be correct", ignore_attr = TRUE) # dput(returned_object$data)
expect_s3_class(actual, "col_spec")
# Project does not have auto-numbering enabled, so study_id should be a character
expect_equal(actual$cols$study_id, readr::col_character())

ds <-
redcap_read_oneshot(
Expand Down Expand Up @@ -103,6 +107,8 @@ test_that("superwide", {
#
# expect_equal(actual, expected=expected, label="The returned col_types should be correct", ignore_attr = TRUE) # dput(returned_object$data)
expect_s3_class(actual, "col_spec")
# Project has auto-numbering enabled, and no dags, so record_id should be an integer
expect_equal(actual$cols$record_id, readr::col_integer())

ds <-
redcap_read_oneshot(
Expand Down

0 comments on commit f6d9d00

Please sign in to comment.