Skip to content

Commit c542841

Browse files
committed
add export_checkbox_label
closes #186
1 parent 697aaae commit c542841

6 files changed

+180
-14
lines changed

R/redcap-read-oneshot.R

+6-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#' @param events_collapsed A single string, where the desired event names are separated by commas. Optional.
1616
#' @param raw_or_label A string (either `'raw'` or `'label'`) that specifies whether to export the raw coded values or the labels for the options of multiple choice fields. Default is `'raw'`.
1717
#' @param raw_or_label_headers A string (either `'raw'` or `'label'` that specifies for the CSV headers whether to export the variable/field names (raw) or the field labels (label). Default is `'raw'`.
18-
# exportCheckboxLabel
18+
#' @param export_checkbox_label specifies the format of checkbox field values specifically when exporting the data as labels. If `raw_or_label` is `'label'` and `export_checkbox_label` is TRUE, the values will be the text displayed to the users. Otherwise, the values will be 0/1.
1919
# returnFormat
2020
#' @param export_survey_fields A boolean that specifies whether to export the survey identifier field (e.g., 'redcap_survey_identifier') or survey timestamp fields (e.g., instrument+'_timestamp') .
2121
#' @param export_data_access_groups A boolean value that specifies whether or not to export the `redcap_data_access_group` field when data access groups are utilized in the project. Default is `FALSE`. See the details below.
@@ -82,7 +82,7 @@ redcap_read_oneshot <- function(
8282
events = NULL, events_collapsed = "",
8383
raw_or_label = "raw",
8484
raw_or_label_headers = "raw",
85-
# exportCheckboxLabel
85+
export_checkbox_label = FALSE, # exportCheckboxLabel
8686
# returnFormat
8787
export_survey_fields = FALSE,
8888
export_data_access_groups = FALSE,
@@ -107,7 +107,7 @@ redcap_read_oneshot <- function(
107107
checkmate::assert_subset( raw_or_label , c("raw", "label"))
108108
checkmate::assert_character(raw_or_label_headers , any.missing=F, len=1)
109109
checkmate::assert_subset( raw_or_label_headers , c("raw", "label"))
110-
# exportCheckboxLabel
110+
checkmate::assert_logical( export_checkbox_label , any.missing=F, len=1)
111111
# returnFormat
112112
checkmate::assert_logical( export_survey_fields , any.missing=F, len=1)
113113
checkmate::assert_logical( export_data_access_groups , any.missing=F, len=1)
@@ -133,7 +133,9 @@ redcap_read_oneshot <- function(
133133
if( any(grepl("[A-Z]", fields_collapsed)) )
134134
warning("The fields passed to REDCap appear to have at least uppercase letter. REDCap variable names are snake case.")
135135

136+
export_checkbox_label_string <- ifelse(export_checkbox_label , "true", "false")
136137
export_data_access_groups_string <- ifelse(export_data_access_groups, "true", "false")
138+
137139
export_survey_fields <- tolower(as.character(export_survey_fields))
138140

139141
post_body <- list(
@@ -143,6 +145,7 @@ redcap_read_oneshot <- function(
143145
type = 'flat',
144146
rawOrLabel = raw_or_label,
145147
rawOrLabelHeaders = raw_or_label_headers,
148+
exportCheckboxLabel = export_checkbox_label_string,
146149
exportSurveyFields = export_survey_fields,
147150
exportDataAccessGroups = export_data_access_groups_string,
148151
# records = ifelse(nchar(records_collapsed) > 0, records_collapsed , NULL),

R/redcap-read.R

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#' @param events_collapsed A single string, where the desired event names are separated by commas. Optional.
2222
#' @param raw_or_label A string (either 'raw` or 'label' that specifies whether to export the raw coded values or the labels for the options of multiple choice fields. Default is `'raw'`.
2323
#' @param raw_or_label_headers A string (either `'raw'` or `'label'` that specifies for the CSV headers whether to export the variable/field names (raw) or the field labels (label). Default is `'raw'`.
24-
# exportCheckboxLabel
24+
#' @param export_checkbox_label specifies the format of checkbox field values specifically when exporting the data as labels. If `raw_or_label` is `'label'` and `export_checkbox_label` is TRUE, the values will be the text displayed to the users. Otherwise, the values will be 0/1.
2525
# returnFormat
2626
#' @param export_survey_fields A boolean that specifies whether to export the survey identifier field (e.g., 'redcap_survey_identifier') or survey timestamp fields (e.g., instrument+'_timestamp') .
2727
#' @param export_data_access_groups A boolean value that specifies whether or not to export the `redcap_data_access_group` field when data access groups are utilized in the project. Default is `FALSE`. See the details below.
@@ -83,7 +83,7 @@ redcap_read <- function(
8383
events = NULL, events_collapsed = "",
8484
raw_or_label = "raw",
8585
raw_or_label_headers = "raw",
86-
# exportCheckboxLabel
86+
export_checkbox_label = FALSE, # exportCheckboxLabel
8787
# returnFormat
8888
export_survey_fields = FALSE,
8989
export_data_access_groups = FALSE,
@@ -105,7 +105,7 @@ redcap_read <- function(
105105
checkmate::assert_subset( raw_or_label , c("raw", "label"))
106106
checkmate::assert_character(raw_or_label_headers , any.missing=F, len=1)
107107
checkmate::assert_subset( raw_or_label_headers , c("raw", "label"))
108-
# exportCheckboxLabel
108+
checkmate::assert_logical( export_checkbox_label , any.missing=F, len=1)
109109
# returnFormat
110110
# export_survey_fields
111111
checkmate::assert_logical( export_data_access_groups , any.missing=F, len=1)
@@ -201,6 +201,7 @@ redcap_read <- function(
201201
export_data_access_groups = export_data_access_groups,
202202
raw_or_label = raw_or_label,
203203
raw_or_label_headers = raw_or_label_headers,
204+
export_checkbox_label = export_checkbox_label,
204205
guess_type = guess_type,
205206
verbose = verbose,
206207
config_options = config_options

man/redcap_read.Rd

+6-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/redcap_read_oneshot.Rd

+6-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-read-batch-simple.R

+69-1
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,6 @@ test_that("All Records -label", {
400400
expect_true(returned_object2$fields_collapsed=="", "A subset of fields was not requested.")
401401
expect_match(returned_object2$outcome_messages, regexp=expected_outcome_message, perl=TRUE)
402402
})
403-
404403
test_that("All Records -label header", {
405404
testthat::skip_on_cran()
406405
expected_data_frame <- structure(list(`Study ID` = c(1, 2, 3, 4, 5), `First Name` = c("Nutmeg",
@@ -488,6 +487,75 @@ test_that("All Records -label header", {
488487
expect_match(returned_object$outcome_message, regexp=expected_outcome_message, perl=TRUE)
489488
expect_true(returned_object$success)
490489
})
490+
test_that("All Records -export_checkbox_label", {
491+
testthat::skip_on_cran()
492+
expected_data_frame <- structure(
493+
list(record_id = c(1, 2, 3, 4, 5), name_first = c("Nutmeg",
494+
"Tumtum", "Marcus", "Trudy", "John Lee"), name_last = c("Nutmouse",
495+
"Nutmouse", "Wood", "DAG", "Walker"), address = c("14 Rose Cottage St.\nKenning UK, 323232",
496+
"14 Rose Cottage Blvd.\nKenning UK 34243", "243 Hill St.\nGuthrie OK 73402",
497+
"342 Elm\nDuncanville TX, 75116", "Hotel Suite\nNew Orleans LA, 70115"
498+
), telephone = c("(405) 321-1111", "(405) 321-2222", "(405) 321-3333",
499+
"(405) 321-4444", "(405) 321-5555"), email = c("[email protected]",
500+
501+
), dob = structure(c(12294, 12121, -13051, -6269, -5375), class = "Date"),
502+
age = c(11, 11, 80, 61, 59), sex = c("Female", "Male", "Male",
503+
"Female", "Male"), demographics_complete = c("Complete",
504+
"Complete", "Complete", "Complete", "Complete"), height = c(7,
505+
6, 180, 165, 193.04), weight = c(1, 1, 80, 54, 104), bmi = c(204.1,
506+
277.8, 24.7, 19.8, 27.9), comments = c("Character in a book, with some guessing",
507+
"A mouse character from a good book", "completely made up",
508+
"This record doesn't have a DAG assigned\n\nSo call up Trudy on the telephone\nSend her a letter in the mail",
509+
"Had a hand for trouble and a eye for cash\n\nHe had a gold watch chain and a black mustache"
510+
), mugshot = c("[document]", "[document]", "[document]",
511+
"[document]", "[document]"), health_complete = c("Unverified",
512+
"Incomplete", "Complete", "Complete", "Incomplete"), race___1 = c(NA,
513+
NA, NA, NA, "American Indian/Alaska Native"), race___2 = c(NA,
514+
NA, NA, "Asian", NA), race___3 = c(NA, "Native Hawaiian or Other Pacific Islander",
515+
NA, NA, NA), race___4 = c(NA, NA, "Black or African American",
516+
NA, NA), race___5 = c("White", "White", "White", "White",
517+
NA), race___6 = c(NA, NA, NA, NA, "Unknown / Not Reported"
518+
), ethnicity = c("NOT Hispanic or Latino", "NOT Hispanic or Latino",
519+
"Unknown / Not Reported", "NOT Hispanic or Latino", "Hispanic or Latino"
520+
), race_and_ethnicity_complete = c("Complete", "Incomplete",
521+
"Complete", "Complete", "Complete")), .Names = c("record_id",
522+
"name_first", "name_last", "address", "telephone", "email", "dob",
523+
"age", "sex", "demographics_complete", "height", "weight", "bmi",
524+
"comments", "mugshot", "health_complete", "race___1", "race___2",
525+
"race___3", "race___4", "race___5", "race___6", "ethnicity",
526+
"race_and_ethnicity_complete"), row.names = c(NA, -5L), class = "data.frame"
527+
)
528+
expected_outcome_message <- "\\d+ records and 24 columns were read from REDCap in \\d+(\\.\\d+\\W|\\W)seconds\\."
529+
530+
###########################
531+
## Default Batch size
532+
expect_message(
533+
regexp = expected_outcome_message,
534+
returned_object1 <- redcap_read(redcap_uri=credential$redcap_uri, token=credential$token, raw_or_label="label", export_checkbox_label=T, verbose=T)
535+
)
536+
expect_equal(returned_object1$data, expected=expected_data_frame, label="The returned data.frame should be correct") # dput(returned_object1$data)
537+
expect_true(returned_object1$success)
538+
expect_match(returned_object1$status_codes, regexp="200", perl=TRUE)
539+
expect_true(returned_object1$records_collapsed=="", "A subset of records was not requested.")
540+
expect_true(returned_object1$fields_collapsed=="", "A subset of fields was not requested.")
541+
expect_true(returned_object1$filter_logic=="", "A filter was not specified.")
542+
expect_match(returned_object1$outcome_messages, regexp=expected_outcome_message, perl=TRUE)
543+
544+
###########################
545+
## Tiny Batch size
546+
expect_message(
547+
regexp = expected_outcome_message,
548+
returned_object2 <- redcap_read(redcap_uri=credential$redcap_uri, token=credential$token, raw_or_label="label", export_checkbox_label=T, verbose=T, batch_size=2)
549+
)
550+
551+
expect_equal(returned_object2$data, expected=expected_data_frame, label="The returned data.frame should be correct") # dput(returned_object2$data)
552+
expect_true(returned_object2$success)
553+
expect_match(returned_object2$status_codes, regexp="200", perl=TRUE)
554+
expect_true(returned_object2$records_collapsed=="", "A subset of records was not requested.")
555+
expect_true(returned_object2$fields_collapsed=="", "A subset of fields was not requested.")
556+
expect_match(returned_object2$outcome_messages, regexp=expected_outcome_message, perl=TRUE)
557+
})
558+
491559

492560
test_that("Filter - numeric", {
493561
testthat::skip_on_cran()

0 commit comments

Comments
 (0)