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 1/4] 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") + }) + +} From 4a06d00a0c5e50650d8c2f6c9998f70c83ad9d2c Mon Sep 17 00:00:00 2001 From: "Paul W. Egeler, MS, GStat" Date: Fri, 26 Jul 2019 17:30:46 -0400 Subject: [PATCH 2/4] Incrementing to v0.2.1 --- R/DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/DESCRIPTION b/R/DESCRIPTION index 275ba9d..21296c2 100644 --- a/R/DESCRIPTION +++ b/R/DESCRIPTION @@ -1,6 +1,6 @@ Package: REDCapRITS Title: REDCap Repeating Instrument Table Splitter -Version: 0.2.0 +Version: 0.2.1 Authors@R: c( person("Paul", "Egeler", email = "paul.egeler@spectrumhealth.org", role = c("aut", "cre")), person("Spectrum Health, Grand Rapids, MI", role = "cph")) From a83221432ff402d8412c0a1f470ce36bd8ea3ae1 Mon Sep 17 00:00:00 2001 From: "Paul W. Egeler, M.S., GStat" Date: Mon, 29 Jul 2019 12:30:52 -0400 Subject: [PATCH 3/4] Cleaning up code for #12 test now compares read_csv and read.csv output directly. --- R/R/REDCap_split.r | 6 +--- R/R/process_user_input.r | 2 +- R/tests/testthat/test-csv-exports.R | 43 +++++++++++++++++++++++++++-- R/tests/testthat/test-readr.R | 22 --------------- 4 files changed, 43 insertions(+), 30 deletions(-) delete mode 100644 R/tests/testthat/test-readr.R diff --git a/R/R/REDCap_split.r b/R/R/REDCap_split.r index 685c1ed..3f516ac 100644 --- a/R/R/REDCap_split.r +++ b/R/R/REDCap_split.r @@ -87,11 +87,7 @@ REDCap_split <- function(records, # Process user input 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" + metadata <- as.data.frame(process_user_input(metadata)) # See issue #12 # Get the variable names in the dataset vars_in_data <- names(records) diff --git a/R/R/process_user_input.r b/R/R/process_user_input.r index 5a4a3ac..fe6de68 100644 --- a/R/R/process_user_input.r +++ b/R/R/process_user_input.r @@ -13,7 +13,7 @@ process_user_input.default <- function(x, ...) { } process_user_input.data.frame <- function(x, ...) { - x + x } process_user_input.character <- function(x, ...) { diff --git a/R/tests/testthat/test-csv-exports.R b/R/tests/testthat/test-csv-exports.R index 098e585..9e42432 100644 --- a/R/tests/testthat/test-csv-exports.R +++ b/R/tests/testthat/test-csv-exports.R @@ -13,10 +13,10 @@ records <- read.csv( ) ) +redcap_output_csv1 <- REDCap_split(records, metadata) + # Test that basic CSV export matches reference ------------------------------ test_that("CSV export matches reference", { - redcap_output_csv1 <- REDCap_split(records, metadata) - expect_known_hash(redcap_output_csv1, "f74558d1939c17d9ff0e08a19b956e26") }) @@ -29,3 +29,42 @@ if (requireNamespace("Hmisc", quietly = TRUE)) { }) } + +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" + ) + ) + + redcap_output_readr <- REDCap_split(records, metadata) + + expect_matching_elements <- function(FUN) { + FUN <- match.fun(FUN) + expect_identical( + lapply(redcap_output_readr, FUN), + lapply(redcap_output_csv1, FUN) + ) + } + + test_that("Result of data read in with `readr` will match result with `read.csv`", { + + # The list itself + expect_identical(length(redcap_output_readr), length(redcap_output_csv1)) + expect_identical(names(redcap_output_readr), names(redcap_output_csv1)) + + # Each element of the list + expect_matching_elements(names) + expect_matching_elements(dim) + }) + +} diff --git a/R/tests/testthat/test-readr.R b/R/tests/testthat/test-readr.R deleted file mode 100644 index 1781eb5..0000000 --- a/R/tests/testthat/test-readr.R +++ /dev/null @@ -1,22 +0,0 @@ -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") - }) - -} From 0357e15d57d1d99fe7b7dd2f83e59f274ec357b1 Mon Sep 17 00:00:00 2001 From: pegeler Date: Wed, 31 Jul 2019 23:19:54 -0400 Subject: [PATCH 4/4] updating NEWS.md with info on v0.2.1 --- R/NEWS.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/R/NEWS.md b/R/NEWS.md index e79fe06..fb6157c 100644 --- a/R/NEWS.md +++ b/R/NEWS.md @@ -1,4 +1,8 @@ -# REDCapRITS 0.2.0 (Release date: 2019-??-??) +# REDCapRITS 0.2.1 (Release date: 2019-07-26) + + * [bug] Can now accept `"tbl_df"` objects and `NA`s in the `redcap_repeat_instrument` field. (#12) + +# REDCapRITS 0.2.0 (Release date: 2019-07-08) * [feature] User can now separate each form into its own data.frame, regardless if it is a repeating instrument or not. (#10) * [bug] Handles auto-generated form timestamp fields.