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
8 changes: 6 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)$data


# 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")
.dags <- nrow(d_dags) > 0
.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,13 @@ 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"),
autonumber & !dags ~ paste0("col_integer()" , "~~record_autonumbering is enabled for the project"),
pwildenhain marked this conversation as resolved.
Show resolved Hide resolved
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