Skip to content

Commit

Permalink
Include in-text survey items when splitting
Browse files Browse the repository at this point in the history
Before survey items were only found by matching /"^(?:Pre|Post)survey_"/.
Instead, a new `survey_item_references` is generated from `survey_item_references.csv` using the script in `survey_item_references.r`.
This list of reference IDs is used when splitting out the survey items.
  • Loading branch information
adamblake committed Nov 10, 2020
1 parent 0af547b commit b55bff0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
15 changes: 13 additions & 2 deletions R/split_responses.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.*")
}
Binary file added R/sysdata.rda
Binary file not shown.
8 changes: 8 additions & 0 deletions data-raw/survey_item_references.r
Original file line number Diff line number Diff line change
@@ -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)
10 changes: 5 additions & 5 deletions tests/testthat/test-split_responses.R
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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", {
Expand Down

0 comments on commit b55bff0

Please sign in to comment.