mirror of
https://github.com/agdamsbo/REDCapCAST.git
synced 2026-06-19 13:17:30 +02:00
linting
This commit is contained in:
parent
a0730cb41c
commit
9e33057c06
32 changed files with 456 additions and 340 deletions
|
|
@ -7,22 +7,23 @@ library(magrittr)
|
|||
library(jsonlite)
|
||||
|
||||
|
||||
ref_data_location <- function(x) file.path("tests","testthat","data", x)
|
||||
ref_data_location <- function(x) file.path("tests", "testthat", "data", x)
|
||||
|
||||
# RCurl -------------------------------------------------------------------
|
||||
|
||||
REDCap_split(
|
||||
ref_data_location("ExampleProject_records.json") %>% fromJSON,
|
||||
ref_data_location("ExampleProject_metadata.json") %>% fromJSON
|
||||
) %>% digest
|
||||
ref_data_location("ExampleProject_records.json") %>% fromJSON(),
|
||||
ref_data_location("ExampleProject_metadata.json") %>% fromJSON()
|
||||
) %>% digest()
|
||||
|
||||
|
||||
# Basic CSV ---------------------------------------------------------------
|
||||
|
||||
REDCap_split(
|
||||
ref_data_location("ExampleProject_DATA_2018-06-07_1129.csv") %>% read.csv,
|
||||
ref_data_location("ExampleProject_DataDictionary_2018-06-07.csv") %>% read.csv
|
||||
) %>% digest
|
||||
ref_data_location("ExampleProject_DATA_2018-06-07_1129.csv") %>% read.csv(),
|
||||
ref_data_location("ExampleProject_DataDictionary_2018-06-07.csv") %>%
|
||||
read.csv()
|
||||
) %>% digest()
|
||||
|
||||
# REDCap R Export ---------------------------------------------------------
|
||||
|
||||
|
|
@ -30,10 +31,11 @@ source("tests/testthat/helper-ExampleProject_R_2018-06-07_1129.r")
|
|||
|
||||
REDCap_split(
|
||||
ref_data_location("ExampleProject_DATA_2018-06-07_1129.csv") %>%
|
||||
read.csv %>%
|
||||
REDCap_process_csv,
|
||||
ref_data_location("ExampleProject_DataDictionary_2018-06-07.csv") %>% read.csv
|
||||
) %>% digest
|
||||
read.csv() %>%
|
||||
REDCap_process_csv(),
|
||||
ref_data_location("ExampleProject_DataDictionary_2018-06-07.csv") %>%
|
||||
read.csv()
|
||||
) %>% digest()
|
||||
|
||||
# Longitudinal data from @pbchase; Issue #7 -------------------------------
|
||||
|
||||
|
|
@ -41,9 +43,10 @@ file_paths <- vapply(
|
|||
c(
|
||||
records = "WARRIORtestForSoftwa_DATA_2018-06-21_1431.csv",
|
||||
metadata = "WARRIORtestForSoftwareUpgrades_DataDictionary_2018-06-21.csv"
|
||||
), FUN.VALUE = "character", ref_data_location
|
||||
),
|
||||
FUN.VALUE = "character", ref_data_location
|
||||
)
|
||||
|
||||
redcap <- lapply(file_paths, read.csv, stringsAsFactors = FALSE)
|
||||
redcap[["metadata"]] <- with(redcap, metadata[metadata[,1] > "",])
|
||||
with(redcap, REDCap_split(records, metadata)) %>% digest
|
||||
redcap[["metadata"]] <- with(redcap, metadata[metadata[, 1] > "", ])
|
||||
with(redcap, REDCap_split(records, metadata)) %>% digest()
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
REDCap_process_csv <- function(data) {
|
||||
#Load Hmisc library
|
||||
# Load Hmisc library
|
||||
if (!requireNamespace("Hmisc", quietly = TRUE)) {
|
||||
stop("This test requires the 'Hmisc' package")
|
||||
}
|
||||
|
|
@ -36,13 +36,13 @@ REDCap_process_csv <- function(data) {
|
|||
Hmisc::label(data$color) <- "Color"
|
||||
Hmisc::label(data$customer) <- "Customer Name"
|
||||
Hmisc::label(data$sale_complete) <- "Complete?"
|
||||
#Setting Units
|
||||
# Setting Units
|
||||
|
||||
|
||||
#Setting Factors(will create new variable for factors)
|
||||
# Setting Factors(will create new variable for factors)
|
||||
data$redcap_repeat_instrument.factor <-
|
||||
factor(data$redcap_repeat_instrument, levels <-
|
||||
c("sale"))
|
||||
c("sale"))
|
||||
data$cyl.factor <-
|
||||
factor(data$cyl, levels <- c("3", "4", "5", "6", "7", "8"))
|
||||
data$vs.factor <- factor(data$vs, levels <- c("1", "0"))
|
||||
|
|
@ -50,36 +50,36 @@ REDCap_process_csv <- function(data) {
|
|||
data$gear.factor <- factor(data$gear, levels <- c("3", "4", "5"))
|
||||
data$carb.factor <-
|
||||
factor(data$carb, levels <-
|
||||
c("1", "2", "3", "4", "5", "6", "7", "8"))
|
||||
c("1", "2", "3", "4", "5", "6", "7", "8"))
|
||||
data$color_available___red.factor <-
|
||||
factor(data$color_available___red, levels <-
|
||||
c("0", "1"))
|
||||
c("0", "1"))
|
||||
data$color_available___green.factor <-
|
||||
factor(data$color_available___green, levels <-
|
||||
c("0", "1"))
|
||||
c("0", "1"))
|
||||
data$color_available___blue.factor <-
|
||||
factor(data$color_available___blue, levels <-
|
||||
c("0", "1"))
|
||||
c("0", "1"))
|
||||
data$color_available___black.factor <-
|
||||
factor(data$color_available___black, levels <-
|
||||
c("0", "1"))
|
||||
c("0", "1"))
|
||||
data$motor_trend_cars_complete.factor <-
|
||||
factor(data$motor_trend_cars_complete, levels <-
|
||||
c("0", "1", "2"))
|
||||
c("0", "1", "2"))
|
||||
data$letter_group___a.factor <-
|
||||
factor(data$letter_group___a, levels <-
|
||||
c("0", "1"))
|
||||
c("0", "1"))
|
||||
data$letter_group___b.factor <-
|
||||
factor(data$letter_group___b, levels <-
|
||||
c("0", "1"))
|
||||
c("0", "1"))
|
||||
data$letter_group___c.factor <-
|
||||
factor(data$letter_group___c, levels <-
|
||||
c("0", "1"))
|
||||
c("0", "1"))
|
||||
data$choice.factor <-
|
||||
factor(data$choice, levels <- c("choice1", "choice2"))
|
||||
data$grouping_complete.factor <-
|
||||
factor(data$grouping_complete, levels <-
|
||||
c("0", "1", "2"))
|
||||
c("0", "1", "2"))
|
||||
data$color.factor <-
|
||||
factor(data$color, levels <- c("1", "2", "3", "4"))
|
||||
data$sale_complete.factor <-
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
|
||||
|
||||
# Set up the path and data -------------------------------------------------
|
||||
metadata <- read.csv(
|
||||
get_data_location("ExampleProject_DataDictionary_2018-06-07.csv"),
|
||||
|
|
@ -8,7 +6,8 @@ metadata <- read.csv(
|
|||
|
||||
records <-
|
||||
read.csv(get_data_location("ExampleProject_DATA_2018-06-07_1129.csv"),
|
||||
stringsAsFactors = TRUE)
|
||||
stringsAsFactors = TRUE
|
||||
)
|
||||
|
||||
redcap_output_csv1 <- REDCap_split(records, metadata)
|
||||
|
||||
|
|
@ -19,20 +18,21 @@ test_that("CSV export matches reference", {
|
|||
|
||||
# Test that REDCap_split can handle a focused dataset
|
||||
|
||||
records_red <- records[!records$redcap_repeat_instrument == "sale",
|
||||
!names(records) %in%
|
||||
metadata$field_name[metadata$form_name == "sale"] &
|
||||
!names(records) == "sale_complete"]
|
||||
records_red <- records[
|
||||
!records$redcap_repeat_instrument == "sale",
|
||||
!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)
|
||||
|
||||
redcap_output_red <- REDCap_split(records_red, metadata)
|
||||
|
||||
|
||||
test_that("REDCap_split handles subset dataset",
|
||||
{
|
||||
testthat::expect_length(redcap_output_red, 1)
|
||||
})
|
||||
test_that("REDCap_split handles subset dataset", {
|
||||
testthat::expect_length(redcap_output_red, 1)
|
||||
})
|
||||
|
||||
|
||||
# Test that R code enhanced CSV export matches reference --------------------
|
||||
|
|
@ -47,35 +47,40 @@ if (requireNamespace("Hmisc", quietly = TRUE)) {
|
|||
|
||||
|
||||
if (requireNamespace("readr", quietly = TRUE)) {
|
||||
|
||||
metadata <-
|
||||
readr::read_csv(get_data_location(
|
||||
"ExampleProject_DataDictionary_2018-06-07.csv"))
|
||||
"ExampleProject_DataDictionary_2018-06-07.csv"
|
||||
))
|
||||
|
||||
records <-
|
||||
readr::read_csv(get_data_location(
|
||||
"ExampleProject_DATA_2018-06-07_1129.csv"))
|
||||
"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))
|
||||
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)
|
||||
})
|
||||
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)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,20 +1,22 @@
|
|||
test_that("strsplitx works", {
|
||||
expect_equal(2 * 2, 4)
|
||||
test <- c("12 months follow-up", "3 steps", "mRS 6 weeks", "Counting to 231 now")
|
||||
expect_length(strsplitx(test,"[0-9]",type="around")[[1]],3)
|
||||
test <- c("12 months follow-up", "3 steps", "mRS 6 weeks",
|
||||
"Counting to 231 now")
|
||||
expect_length(strsplitx(test, "[0-9]", type = "around")[[1]], 3)
|
||||
|
||||
expect_equal(strsplitx(test,"[0-9]",type="classic")[[2]][1],"")
|
||||
expect_length(strsplitx(test,"[0-9]",type="classic")[[4]],4)
|
||||
expect_equal(strsplitx(test, "[0-9]", type = "classic")[[2]][1], "")
|
||||
expect_length(strsplitx(test, "[0-9]", type = "classic")[[4]], 4)
|
||||
|
||||
expect_length(strsplitx(test,"[0-9]",type="classic")[[4]],4)
|
||||
expect_length(strsplitx(test, "[0-9]", type = "classic")[[4]], 4)
|
||||
})
|
||||
|
||||
test_that("d2w works", {
|
||||
expect_length(d2w(c(2:8, 21)), 8)
|
||||
|
||||
expect_length(d2w(c(2:8,21)),8)
|
||||
expect_equal(d2w(data.frame(2:7, 3:8, 1),
|
||||
lang = "da",
|
||||
neutrum = TRUE
|
||||
)[1, 3], "et")
|
||||
|
||||
expect_equal(d2w(data.frame(2:7,3:8,1),lang="da",
|
||||
neutrum=TRUE)[1,3],"et")
|
||||
|
||||
expect_equal(d2w(list(2:8,c(2,6,4,23),2), everything=T)[[2]][4],"two three")
|
||||
expect_equal(d2w(list(2:8, c(2, 6, 4, 23), 2), everything = T)[[2]][4], "two three")
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue