Skip to content

Commit

Permalink
Merge pull request #344 from OuhscBbmc/dev
Browse files Browse the repository at this point in the history
adjust for readr 2.0
  • Loading branch information
wibeasley authored Jul 21, 2021
2 parents c5bce6a + af812ea commit aeeae25
Show file tree
Hide file tree
Showing 17 changed files with 138 additions and 130 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Description: Encapsulates functions to streamline calls from R to the REDCap
University. The Application Programming Interface (API) offers an avenue
to access and modify data programmatically, improving the capacity for
literate and reproducible programming.
Version: 0.11.1.9004
Version: 0.11.1.9005
Authors@R: c(person("Will", "Beasley", role = c("aut", "cre"), email =
"[email protected]", comment = c(ORCID = "0000-0002-5613-5006")),
person("David", "Bard", role = "ctb"),
Expand All @@ -30,7 +30,7 @@ Imports:
httr (>= 1.4.0),
magrittr (>= 1.5),
methods,
readr (>= 1.3.1),
readr (>= 2.0),
rlang (>= 0.4),
tibble (>= 2.0),
tidyr (>= 1.0)
Expand Down
4 changes: 2 additions & 2 deletions R/kernel-api.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
#'
#' # Consume the results in a few different ways.
#' kernel$result
#' read.csv(text=kernel$raw_text, stringsAsFactors=FALSE)
#' as.list(read.csv(text=kernel$raw_text, stringsAsFactors=FALSE))
#' read.csv(text=kernel$raw_text)
#' as.list(read.csv(text=kernel$raw_text))

kernel_api <- function(
redcap_uri,
Expand Down
6 changes: 5 additions & 1 deletion R/project-simple.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ populate_project_simple <- function(batch = FALSE) {
# utils::write.csv(returned_object_metadata$data, file="./inst/test-data/project-simple/simple-metadata.csv", row.names=FALSE)

# Read in the data in R's memory from a csv file.
ds_to_write <- readr::read_csv(path_in_simple)
ds_to_write <-
readr::read_csv(
path_in_simple,
show_col_types = FALSE
)
# ds_to_write <- utils::read.csv(file="./inst/test-data/project-simple/simple-data.csv", stringsAsFactors=FALSE)

# Remove the calculated variables.
Expand Down
5 changes: 3 additions & 2 deletions R/redcap-metadata-read.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ redcap_metadata_read <- function(
# Convert the raw text to a dataset.
ds <-
readr::read_csv(
kernel$raw_text,
col_types = col_types
file = I(kernel$raw_text),
col_types = col_types,
show_col_types = FALSE
),
# Don't print the warning in the try block. Print it below,
# where it's under the control of the caller.
Expand Down
8 changes: 6 additions & 2 deletions R/redcap-read-oneshot-eav.R
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,11 @@ redcap_read_oneshot_eav <- function(
if (kernel$success) {
try (
{
ds_eav <- readr::read_csv(kernel$raw_text)
ds_eav <-
readr::read_csv(
file = I(kernel$raw_text),
show_col_types = FALSE
)

ds_metadata_expanded <-
ds_metadata %>%
Expand Down Expand Up @@ -312,7 +316,7 @@ redcap_read_oneshot_eav <- function(

ds_2 <-
ds %>%
dplyr::mutate_if(is.character, type.convert) %>%
dplyr::mutate_if(is.character, ~type.convert(., as.is = FALSE)) %>%
dplyr::mutate_if(is.factor , as.character)
}, #Convert the raw text to a dataset.
silent = TRUE #Don't print the warning in the try block. Print it below, where it's under the control of the caller.
Expand Down
12 changes: 8 additions & 4 deletions R/redcap-read-oneshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@
#'
#' If you do not pass in this `export_data_access_groups` value, it will default
#' to `FALSE`. The following is from the API help page for version 10.5.1:
#' >This flag is only viable if the user whose token is being used to make the
#' *This flag is only viable if the user whose token is being used to make the
#' API request is *not* in a data access group. If the user is in a group,
#' then this flag will revert to its default value.
#' then this flag will revert to its default value*.
#'
#' @author Will Beasley
#'
Expand Down Expand Up @@ -257,8 +257,12 @@ redcap_read_oneshot <- function(
try(
# Convert the raw text to a dataset.
ds <-
kernel$raw_text %>%
readr::read_csv(col_types = col_types, guess_max = guess_max) %>%
readr::read_csv(
file = I(kernel$raw_text),
col_types = col_types,
guess_max = guess_max,
show_col_types = FALSE
) %>%
as.data.frame(),

# Don't print the warning in the try block. Print it below,
Expand Down
8 changes: 6 additions & 2 deletions R/redcap-report.R
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,12 @@ redcap_report <- function(
try(
# Convert the raw text to a dataset.
ds <-
kernel$raw_text %>%
readr::read_csv(col_types = col_types, guess_max = guess_max) %>%
readr::read_csv(
file = I(kernel$raw_text),
col_types = col_types,
guess_max = guess_max,
show_col_types = FALSE
) %>%
as.data.frame(),

# Don't print the warning in the try block. Print it below,
Expand Down
5 changes: 3 additions & 2 deletions R/redcap-users-export.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ redcap_users_export <- function(
try(
{ # readr::spec_csv(kernel$raw_text)
ds_combined <- readr::read_csv(
file = kernel$raw_text,
col_types = col_types
file = I(kernel$raw_text),
col_types = col_types,
show_col_types = FALSE
)

# Remove the readr's `spec` attribute about the column names & types.
Expand Down
6 changes: 5 additions & 1 deletion R/redcap-variables.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ redcap_variables <- function(
if (kernel$success) {
try(
{
ds <- readr::read_csv(file = kernel$raw_text)
ds <-
readr::read_csv(
file = I(kernel$raw_text),
show_col_types = FALSE
)
}, #Convert the raw text to a dataset.
silent = TRUE
# Don't print the warning in the try block. Print it below, where
Expand Down
7 changes: 4 additions & 3 deletions R/retrieve-credential.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,10 @@ retrieve_credential_local <- function(
)

ds_credentials <- readr::read_csv(
file = path_credential,
col_types = col_types,
comment = "#"
file = path_credential,
col_types = col_types,
comment = "#",
show_col_types = FALSE
)

# Check that it's a data.frame with valid variable names
Expand Down
29 changes: 1 addition & 28 deletions inst/test-data/specific-redcapr/read-batch-simple/col_types.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,4 @@ structure(list(record_id = 1:5, name_first = c("Nutmeg", "Tumtum",
FALSE, FALSE), race___5 = c(TRUE, TRUE, TRUE, TRUE, FALSE
), race___6 = c(FALSE, FALSE, FALSE, FALSE, TRUE), ethnicity = c(1,
1, 0, 1, 2), interpreter_needed = c(0, 0, 1, NA, 0), race_and_ethnicity_complete = c(2,
0, 2, 2, 2)), class = "data.frame", row.names = c(NA, -5L
), spec = structure(list(cols = list(record_id = structure(list(), class = c("collector_integer",
"collector")), name_first = structure(list(), class = c("collector_character",
"collector")), name_last = structure(list(), class = c("collector_character",
"collector")), address = structure(list(), class = c("collector_character",
"collector")), telephone = structure(list(), class = c("collector_character",
"collector")), email = structure(list(), class = c("collector_character",
"collector")), dob = structure(list(format = ""), class = c("collector_date",
"collector")), age = structure(list(), class = c("collector_double",
"collector")), sex = structure(list(), class = c("collector_double",
"collector")), demographics_complete = structure(list(), class = c("collector_double",
"collector")), height = structure(list(), class = c("collector_double",
"collector")), weight = structure(list(), class = c("collector_double",
"collector")), bmi = structure(list(), class = c("collector_double",
"collector")), comments = structure(list(), class = c("collector_character",
"collector")), mugshot = structure(list(), class = c("collector_character",
"collector")), health_complete = structure(list(), class = c("collector_double",
"collector")), race___1 = structure(list(), class = c("collector_logical",
"collector")), race___2 = structure(list(), class = c("collector_logical",
"collector")), race___3 = structure(list(), class = c("collector_logical",
"collector")), race___4 = structure(list(), class = c("collector_logical",
"collector")), race___5 = structure(list(), class = c("collector_logical",
"collector")), race___6 = structure(list(), class = c("collector_logical",
"collector")), ethnicity = structure(list(), class = c("collector_double",
"collector")), interpreter_needed = structure(list(), class = c("collector_double",
"collector")), race_and_ethnicity_complete = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1L), class = "col_spec"))
0, 2, 2, 2)), row.names = c(NA, -5L), class = "data.frame")
4 changes: 2 additions & 2 deletions man/kernel_api.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion man/redcap_read_oneshot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion tests/testthat/test-metadata-write.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ path_in <- system.file(
"test-data/project-simple/simple-metadata.csv",
package = "REDCapR"
)
dictionary_to_write <- readr::read_csv(path_in, col_types = readr::cols(.default = readr::col_character()))
dictionary_to_write <-
readr::read_csv(
file = path_in,
col_types = readr::cols(.default = readr::col_character()),
show_col_types = FALSE
)

test_that("Metadata Write", {
testthat::skip_on_cran()
Expand Down
51 changes: 26 additions & 25 deletions tests/testthat/test-read-batch-simple.R
Original file line number Diff line number Diff line change
Expand Up @@ -295,31 +295,32 @@ test_that("label", {
expect_true(returned_object2$fields_collapsed=="", "A subset of fields was not requested.")
expect_match(returned_object2$outcome_messages, regexp=expected_outcome_message, perl=TRUE)
})
test_that("label-header", {
testthat::skip_on_cran()
path_expected <- "test-data/specific-redcapr/read-batch-simple/label-header.R"

expected_warning <- "Duplicated column names deduplicated: 'Complete\\?' => 'Complete\\?_1' \\[16\\], 'Complete\\?' => 'Complete\\?_2' \\[25\\]"
expected_outcome_message <- "\\d+ records and \\d+ columns were read from REDCap in \\d+(\\.\\d+\\W|\\W)seconds\\."

expect_warning(
regexp = expected_warning,
expect_message(
regexp = expected_outcome_message,
returned_object <- redcap_read(redcap_uri=credential$redcap_uri, token=credential$token, raw_or_label_headers="label")
)
)

if (update_expectation) save_expected(returned_object$data, path_expected)
expected_data_frame <- retrieve_expected(path_expected)

expect_equal(returned_object$data, expected=expected_data_frame, label="The returned data.frame should be correct", ignore_attr = TRUE) # dput(returned_object$data)
expect_match(returned_object$status_codes, regexp="200", perl=TRUE)
expect_true(returned_object$records_collapsed=="", "A subset of records was not requested.")
expect_true(returned_object$fields_collapsed=="", "A subset of fields was not requested.")
expect_match(returned_object$outcome_message, regexp=expected_outcome_message, perl=TRUE)
expect_true(returned_object$success)
})
# This test is removed because the vroom version adds digits to make the columns unique
# test_that("label-header", {
# testthat::skip_on_cran()
# path_expected <- "test-data/specific-redcapr/read-batch-simple/label-header.R"
#
# expected_warning <- "Duplicated column names deduplicated: 'Complete\\?' => 'Complete\\?_1' \\[16\\], 'Complete\\?' => 'Complete\\?_2' \\[25\\]"
# expected_outcome_message <- "\\d+ records and \\d+ columns were read from REDCap in \\d+(\\.\\d+\\W|\\W)seconds\\."
#
# expect_warning(
# regexp = expected_warning,
# expect_message(
# regexp = expected_outcome_message,
# returned_object <- redcap_read(redcap_uri=credential$redcap_uri, token=credential$token, raw_or_label_headers="label")
# )
# )
#
# if (update_expectation) save_expected(returned_object$data, path_expected)
# expected_data_frame <- retrieve_expected(path_expected)
#
# expect_equal(returned_object$data, expected=expected_data_frame, label="The returned data.frame should be correct", ignore_attr = TRUE) # dput(returned_object$data)
# expect_match(returned_object$status_codes, regexp="200", perl=TRUE)
# expect_true(returned_object$records_collapsed=="", "A subset of records was not requested.")
# expect_true(returned_object$fields_collapsed=="", "A subset of fields was not requested.")
# expect_match(returned_object$outcome_message, regexp=expected_outcome_message, perl=TRUE)
# expect_true(returned_object$success)
# })
test_that("export_checkbox_label", {
testthat::skip_on_cran()
path_expected <- "test-data/specific-redcapr/read-batch-simple/export_checkbox_label.R"
Expand Down
54 changes: 28 additions & 26 deletions tests/testthat/test-read-oneshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -204,32 +204,33 @@ test_that("label", {
expect_match(returned_object$outcome_message, regexp=expected_outcome_message, perl=TRUE)
expect_true(returned_object$success)
})
test_that("label-header", {
testthat::skip_on_cran()
path_expected <- "test-data/specific-redcapr/read-oneshot/label-header.R"
expected_warning <- "Duplicated column names deduplicated: 'Complete\\?' => 'Complete\\?_1' \\[\\d+\\], 'Complete\\?' => 'Complete\\?_2' \\[\\d+\\]"
expected_outcome_message <- "\\d+ records and \\d+ columns were read from REDCap in \\d+(\\.\\d+\\W|\\W)seconds\\."

expect_warning(
regexp = expected_warning,
expect_message(
regexp = expected_outcome_message,
returned_object <- redcap_read_oneshot(redcap_uri=credential$redcap_uri, token=credential$token, raw_or_label_headers="label")
)
)

if (update_expectation) save_expected(returned_object$data, path_expected)
expected_data_frame <- retrieve_expected(path_expected)

expect_equal(returned_object$data, expected=expected_data_frame, label="The returned data.frame should be correct", ignore_attr = TRUE) # dput(returned_object$data)
expect_equal(returned_object$status_code, expected=200L)
expect_equal(returned_object$raw_text, expected="", ignore_attr = TRUE) # dput(returned_object$raw_text)
expect_true(returned_object$records_collapsed=="", "A subset of records was not requested.")
expect_true(returned_object$fields_collapsed=="", "A subset of fields was not requested.")
expect_true(returned_object$filter_logic=="", "A filter was not specified.")
expect_match(returned_object$outcome_message, regexp=expected_outcome_message, perl=TRUE)
expect_true(returned_object$success)
})
# This test is removed because the vroom version adds digits to make the columns unique
# test_that("label-header", {
# testthat::skip_on_cran()
# path_expected <- "test-data/specific-redcapr/read-oneshot/label-header.R"
# expected_warning <- "Duplicated column names deduplicated: 'Complete\\?' => 'Complete\\?_1' \\[\\d+\\], 'Complete\\?' => 'Complete\\?_2' \\[\\d+\\]"
# expected_outcome_message <- "\\d+ records and \\d+ columns were read from REDCap in \\d+(\\.\\d+\\W|\\W)seconds\\."
#
# expect_warning(
# regexp = expected_warning,
# expect_message(
# regexp = expected_outcome_message,
# returned_object <- redcap_read_oneshot(redcap_uri=credential$redcap_uri, token=credential$token, raw_or_label_headers="label")
# )
# )
#
# if (update_expectation) save_expected(returned_object$data, path_expected)
# expected_data_frame <- retrieve_expected(path_expected)
#
# expect_equal(returned_object$data, expected=expected_data_frame, label="The returned data.frame should be correct", ignore_attr = TRUE) # dput(returned_object$data)
# expect_equal(returned_object$status_code, expected=200L)
# expect_equal(returned_object$raw_text, expected="", ignore_attr = TRUE) # dput(returned_object$raw_text)
# expect_true(returned_object$records_collapsed=="", "A subset of records was not requested.")
# expect_true(returned_object$fields_collapsed=="", "A subset of fields was not requested.")
# expect_true(returned_object$filter_logic=="", "A filter was not specified.")
# expect_match(returned_object$outcome_message, regexp=expected_outcome_message, perl=TRUE)
# expect_true(returned_object$success)
# })
test_that("export_checkbox_label", {
testthat::skip_on_cran()
path_expected <- "test-data/specific-redcapr/read-oneshot/export_checkbox_label.R"
Expand Down Expand Up @@ -348,3 +349,4 @@ test_that("bad token -Error", {
testthat::expect_equal(returned_object$raw_text, "ERROR: You do not have permissions to use the API")
})
rm(credential)

Loading

0 comments on commit aeeae25

Please sign in to comment.