2018-06-26 05:48:55 +02:00
|
|
|
|
2023-04-13 10:57:04 +02:00
|
|
|
|
2018-06-26 05:48:55 +02:00
|
|
|
# Set up the path and data -------------------------------------------------
|
|
|
|
metadata <- read.csv(
|
2020-06-03 06:40:50 +02:00
|
|
|
get_data_location("ExampleProject_DataDictionary_2018-06-07.csv"),
|
|
|
|
stringsAsFactors = TRUE
|
2018-06-26 05:48:55 +02:00
|
|
|
)
|
|
|
|
|
2023-04-13 10:57:04 +02:00
|
|
|
records <-
|
|
|
|
read.csv(get_data_location("ExampleProject_DATA_2018-06-07_1129.csv"),
|
|
|
|
stringsAsFactors = TRUE)
|
2018-06-26 05:48:55 +02:00
|
|
|
|
2019-07-29 18:30:52 +02:00
|
|
|
redcap_output_csv1 <- REDCap_split(records, metadata)
|
|
|
|
|
2018-06-26 05:48:55 +02:00
|
|
|
# Test that basic CSV export matches reference ------------------------------
|
|
|
|
test_that("CSV export matches reference", {
|
2023-04-14 11:47:23 +02:00
|
|
|
expect_known_hash(redcap_output_csv1, "cb5074a06e1abcf659d60be1016965d2")
|
2018-06-26 05:48:55 +02:00
|
|
|
})
|
|
|
|
|
2023-03-07 15:38:28 +01:00
|
|
|
# Test that REDCap_split can handle a focused dataset
|
|
|
|
|
|
|
|
records_red <- records[!records$redcap_repeat_instrument == "sale",
|
2023-04-13 10:57:04 +02:00
|
|
|
!names(records) %in%
|
|
|
|
metadata$field_name[metadata$form_name == "sale"] &
|
|
|
|
!names(records) == "sale_complete"]
|
|
|
|
records_red$redcap_repeat_instrument <-
|
|
|
|
as.character(records_red$redcap_repeat_instrument)
|
2023-03-07 15:38:28 +01:00
|
|
|
|
|
|
|
redcap_output_red <- REDCap_split(records_red, metadata)
|
|
|
|
|
|
|
|
|
|
|
|
test_that("REDCap_split handles subset dataset",
|
|
|
|
{
|
2023-04-13 10:57:04 +02:00
|
|
|
testthat::expect_length(redcap_output_red, 1)
|
2023-03-07 15:38:28 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
2018-06-26 05:48:55 +02:00
|
|
|
# Test that R code enhanced CSV export matches reference --------------------
|
2018-06-28 23:20:14 +02:00
|
|
|
if (requireNamespace("Hmisc", quietly = TRUE)) {
|
|
|
|
test_that("R code enhanced export matches reference", {
|
2023-01-16 09:49:17 +01:00
|
|
|
redcap_output_csv2 <-
|
|
|
|
REDCap_split(REDCap_process_csv(records), metadata)
|
2018-06-26 05:48:55 +02:00
|
|
|
|
2023-04-14 11:47:23 +02:00
|
|
|
expect_known_hash(redcap_output_csv2, "578dc054e59ec92a21e950042e08ee37")
|
2018-06-28 23:20:14 +02:00
|
|
|
})
|
|
|
|
}
|
2018-06-26 05:48:55 +02:00
|
|
|
|
2019-07-29 18:30:52 +02:00
|
|
|
|
|
|
|
if (requireNamespace("readr", quietly = TRUE)) {
|
|
|
|
|
2023-04-13 10:57:04 +02:00
|
|
|
metadata <-
|
|
|
|
readr::read_csv(get_data_location(
|
|
|
|
"ExampleProject_DataDictionary_2018-06-07.csv"))
|
2019-07-29 18:30:52 +02:00
|
|
|
|
2023-04-13 10:57:04 +02:00
|
|
|
records <-
|
|
|
|
readr::read_csv(get_data_location(
|
|
|
|
"ExampleProject_DATA_2018-06-07_1129.csv"))
|
2019-07-29 18:30:52 +02:00
|
|
|
|
|
|
|
redcap_output_readr <- REDCap_split(records, metadata)
|
|
|
|
|
|
|
|
expect_matching_elements <- function(FUN) {
|
|
|
|
FUN <- match.fun(FUN)
|
2023-01-16 09:49:17 +01:00
|
|
|
expect_identical(lapply(redcap_output_readr, FUN),
|
|
|
|
lapply(redcap_output_csv1, FUN))
|
2019-07-29 18:30:52 +02:00
|
|
|
}
|
|
|
|
|
2023-04-13 10:57:04 +02:00
|
|
|
test_that("Result of data read in with `readr` will
|
|
|
|
match result with `read.csv`",
|
2023-01-16 09:49:17 +01:00
|
|
|
{
|
|
|
|
# The list itself
|
2023-04-13 10:57:04 +02:00
|
|
|
expect_identical(length(redcap_output_readr),
|
|
|
|
length(redcap_output_csv1))
|
|
|
|
expect_identical(names(redcap_output_readr),
|
|
|
|
names(redcap_output_csv1))
|
2019-07-29 18:30:52 +02:00
|
|
|
|
2023-01-16 09:49:17 +01:00
|
|
|
# Each element of the list
|
|
|
|
expect_matching_elements(names)
|
|
|
|
expect_matching_elements(dim)
|
|
|
|
})
|
2019-07-29 18:30:52 +02:00
|
|
|
|
|
|
|
}
|