From cb253af3ba0e60a4afe42c7f97bd9b189fbb52f6 Mon Sep 17 00:00:00 2001 From: "Paul W. Egeler, MS, GStat" Date: Fri, 26 Jul 2019 17:26:33 -0400 Subject: [PATCH] Closing issue #12 test included --- R/DESCRIPTION | 3 ++- R/R/REDCap_split.r | 13 +++++++++++++ R/tests/testthat/test-readr.R | 22 ++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 R/tests/testthat/test-readr.R diff --git a/R/DESCRIPTION b/R/DESCRIPTION index 3163f9a..275ba9d 100644 --- a/R/DESCRIPTION +++ b/R/DESCRIPTION @@ -14,7 +14,8 @@ Suggests: httr, jsonlite, testthat, - Hmisc + Hmisc, + readr License: GPL-3 Encoding: UTF-8 LazyData: true diff --git a/R/R/REDCap_split.r b/R/R/REDCap_split.r index d28a26a..685c1ed 100644 --- a/R/R/REDCap_split.r +++ b/R/R/REDCap_split.r @@ -89,6 +89,10 @@ REDCap_split <- function(records, records <- process_user_input(records) metadata <- process_user_input(metadata) + # Remove "tbl_df" class from metadata, if present, due to difference in + # `[.tbl_df` behavior as compared to `[.data.frame` behavior (see issue #12) + if(inherits(metadata, "tbl_df")) class(metadata) <- "data.frame" + # Get the variable names in the dataset vars_in_data <- names(records) @@ -100,6 +104,15 @@ REDCap_split <- function(records, stop("There are no repeating instruments in this dataset.") } + # Remove NAs from `redcap_repeat_instrument` (see issue #12) + if(any(is.na(records$redcap_repeat_instrument))) { + records$redcap_repeat_instrument <- ifelse( + is.na(records$redcap_repeat_instrument), + "", + as.character(records$redcap_repeat_instrument) + ) + } + # Standardize variable names for metadata names(metadata) <- metadata_names diff --git a/R/tests/testthat/test-readr.R b/R/tests/testthat/test-readr.R new file mode 100644 index 0000000..1781eb5 --- /dev/null +++ b/R/tests/testthat/test-readr.R @@ -0,0 +1,22 @@ +if (requireNamespace("readr", quietly = TRUE)) { + + context("Compatibility with readr") + + metadata <- readr::read_csv( + get_data_location( + "ExampleProject_DataDictionary_2018-06-07.csv" + ) + ) + + records <- readr::read_csv( + get_data_location( + "ExampleProject_DATA_2018-06-07_1129.csv" + ) + ) + + test_that("Data read in with `readr` will return correct result", { + redcap_output_readr <- REDCap_split(records, metadata) + expect_known_hash(redcap_output_readr, "bde303d330fba161ca500c10cfabb693") + }) + +}