Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix coltypes for projects with dags #472

Merged
merged 9 commits into from
Apr 18, 2023
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"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for so much blabbing over one line. Can you check my logic? Wouldn't it be better to change dags to autonumber & dags? Otherwise, I think it would execute for all variables in any project with dags.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries haha. dags will only evaluate to TRUE when the field is the .record_id field AND the project has DAGs (similar to how autonumber already works

https://github.com/pwildenhain/REDCapR/blob/ef2171960807ede2136172e59b866953b7928666/R/redcap-metadata-coltypes.R#L375-L379

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