diff --git a/R/split_responses.R b/R/split_responses.R index d51c446..173c5cf 100644 --- a/R/split_responses.R +++ b/R/split_responses.R @@ -16,8 +16,19 @@ #' @export split_responses <- function(responses) { ensure_columns(responses, "item_id", stop, "Cannot split responses. ") - surveys <- stringr::str_detect(responses[["item_id"]], "^(?:Pre|Post)survey_") - quizzes <- stringr::str_detect(responses[["item_id"]], ".*_Practice_Quiz.*") + surveys <- survey_item_map(responses) + quizzes <- quiz_item_map(responses) groups <- ifelse(surveys, "surveys", ifelse(quizzes, "quizzes", "in_text")) split(tibble::as_tibble(responses), groups) } + + +survey_item_map <- function(responses) { + item_map_lower <- stringr::str_to_lower(survey_item_references[["item_id"]]) + stringr::str_to_lower(responses[["item_id"]]) %in% item_map_lower +} + + +quiz_item_map <- function(responses) { + stringr::str_detect(responses[["item_id"]], ".*_Practice_Quiz.*") +} diff --git a/R/sysdata.rda b/R/sysdata.rda new file mode 100644 index 0000000..9c99812 Binary files /dev/null and b/R/sysdata.rda differ diff --git a/data-raw/survey_item_references.r b/data-raw/survey_item_references.r new file mode 100644 index 0000000..b1af41e --- /dev/null +++ b/data-raw/survey_item_references.r @@ -0,0 +1,8 @@ +survey_item_references <- read.csv('./data-raw/survey_item_references.csv', na.strings = c("", "NA")) + +logicals <- stringr::str_starts(names(survey_item_references), 'included_v') +survey_item_references[logicals] <- lapply(survey_item_references[logicals], as.logical) + +survey_item_references[!logicals] <- lapply(survey_item_references[!logicals], as.character) + +usethis::use_data(survey_item_references, internal = TRUE, overwrite = TRUE) diff --git a/tests/testthat/test-split_responses.R b/tests/testthat/test-split_responses.R index 521329e..3ca0482 100644 --- a/tests/testthat/test-split_responses.R +++ b/tests/testthat/test-split_responses.R @@ -1,6 +1,6 @@ mock_responses <- tibble::tibble( item_id = c( - "Presurvey_101_other_text", "Postsurvey_101_other_text", + "Presurvey_0719_Student_Background", "Postsurvey_0719_Attitudes", "Embedded_0719_Value", "Ch1_Practice_Quiz", "Ch6_Practice_Quiz_2", "literally", "anything", "else" ) @@ -22,16 +22,16 @@ test_that("response tables missing required columns throw informative errors", { ) }) -test_that("pre- and post- survey items end up in surveys", { - expect_identical(split$survey, mock_responses[1:2, ]) +test_that("survey items end up in surveys", { + expect_identical(split$survey, mock_responses[1:3, ]) }) test_that("practice quiz items end up in quizzes", { - expect_identical(split$quizzes, mock_responses[3:4, ]) + expect_identical(split$quizzes, mock_responses[4:5, ]) }) test_that("all other items end up in in_text", { - expect_identical(split$in_text, mock_responses[5:7, ]) + expect_identical(split$in_text, mock_responses[6:8, ]) }) test_that("all responses are accounted for", {