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

tweak to redcap_meta_coltypes() #476

Merged
merged 10 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 <- (1L <= nrow(d_dags$data)) | (grepl("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
4 changes: 2 additions & 2 deletions inst/test-data/project-survey/expected/default.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ NA), prescreening_survey_timestamp = structure(c(1520351563,
NA), first_name = c(NA, NA), last_name = c(NA, NA), address = c(NA,
NA), telephone_1 = c(NA, NA), ethnicity = c(NA, NA), race = c(NA,
NA), sex = c(NA, NA), height = c(NA, NA), weight = c(NA,
NA), participant_info_survey_complete = c(0, 0), participant_morale_questionnaire_timestamp = c(NA,
NA), participant_info_survey_complete = c(0, 0), participant_morale_questionnaire_timestamp = c("[not completed]",
NA), pmq1 = c(NA, NA), pmq2 = c(NA, NA), pmq3 = c(NA, NA),
pmq4 = c(NA, NA), participant_morale_questionnaire_complete = c(0,
0), complete_study = c(NA, NA), withdraw_date = c(NA, NA),
withdraw_reason = c(NA, NA), date_visit_4 = c(NA, NA), discharge_date_4 = c(NA,
NA), discharge_summary_4 = c(NA, NA), study_comments = c(NA,
NA), completion_data_complete = c(0, 0)), row.names = c(NA,
-2L), class = "data.frame")
-2L), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"))
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
1 change: 1 addition & 0 deletions utility/refresh.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ devtools::check( # Equivalent of R-hub
devtools::check_rhub(email="[email protected]")
# devtools::check_win_devel() # CRAN submission policies encourage the development version
# revdepcheck::revdep_check(num_workers = 4)
# usethis::use_release_issue()
# devtools::release(check=FALSE) #Careful, the last question ultimately uploads it to CRAN, where you can't delete/reverse your decision.