Skip to content

[CTIS] Make split_options return list of character missing if all input values are NA #1446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 10 additions & 21 deletions facebook/delphiFacebook/R/variables.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
#' @return list of same length, each entry of which is a vector of selected
#' options
split_options <- function(column) {
return(strsplit(column, ",", fixed = TRUE))
if ( any(!is.na(column)) ) {
return(strsplit(column, ",", fixed = TRUE))
} else {
return(rep(list(NA_character_), length(column)))
}
}

#' Test if a specific selection is selected
Expand Down Expand Up @@ -648,16 +652,8 @@ code_vaccines <- function(input_data, wave) {
if ( all(c("V15a", "V15b") %in% names(input_data)) ) {
# introduced in Wave 11
vaccine_barriers <- coalesce(input_data$V15a, input_data$V15b)

# If the entire column is NA, ifelse() results in a logical vector, not a
# character vector, which confuses split_options; since the result should be
# NA anyway
vaccine_barriers <- as.character(
ifelse(vaccine_barriers == "13", NA_character_, vaccine_barriers)
)
if (any(!is.na(vaccine_barriers))) {
vaccine_barriers <- split_options(vaccine_barriers)
}
vaccine_barriers <- ifelse(vaccine_barriers == "13", NA_character_, vaccine_barriers)
vaccine_barriers <- split_options(vaccine_barriers)

input_data$v_vaccine_barrier_eligible <- is_selected(vaccine_barriers, "1")
input_data$v_vaccine_barrier_no_appointments <- is_selected(vaccine_barriers, "2")
Expand All @@ -677,7 +673,7 @@ code_vaccines <- function(input_data, wave) {
} else if ( all(c("V15c", "V15b") %in% names(input_data)) ) {
# V15c introduced in Wave 12, replacing V15a with clarified wording.
vaccine_barriers <- coalesce(input_data$V15c, input_data$V15b)
vaccine_barriers <- ifelse(vaccine_barriers == "13", NA, vaccine_barriers)
vaccine_barriers <- ifelse(vaccine_barriers == "13", NA_character_, vaccine_barriers)
vaccine_barriers <- split_options(vaccine_barriers)

input_data$v_vaccine_barrier_eligible <- is_selected(vaccine_barriers, "1")
Expand Down Expand Up @@ -769,15 +765,8 @@ code_vaccines <- function(input_data, wave) {

if ( "V15b" %in% names(input_data) ) {
# introduced in Wave 11
# If the entire column is NA, ifelse() results in a logical vector, not a
# character vector, which confuses split_options; since the result should be
# NA anyway
vaccine_barriers <- as.character(
ifelse(input_data$V15b == "13", NA, input_data$V15b)
)
if (any(!is.na(vaccine_barriers))) {
vaccine_barriers <- split_options(vaccine_barriers)
}
vaccine_barriers <- ifelse(input_data$V15b == "13", NA_character_, input_data$V15b)
vaccine_barriers <- split_options(vaccine_barriers)

input_data$v_vaccine_barrier_eligible_tried <- is_selected(vaccine_barriers, "1")
input_data$v_vaccine_barrier_no_appointments_tried <- is_selected(vaccine_barriers, "2")
Expand Down
5 changes: 5 additions & 0 deletions facebook/delphiFacebook/src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

using namespace Rcpp;

#ifdef RCPP_USE_GLOBAL_ROSTREAM
Rcpp::Rostream<true>& Rcpp::Rcout = Rcpp::Rcpp_cout_get();
Rcpp::Rostream<false>& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get();
#endif

// is_selected_cpp
LogicalVector is_selected_cpp(List responses, String target);
RcppExport SEXP _delphiFacebook_is_selected_cpp(SEXP responsesSEXP, SEXP targetSEXP) {
Expand Down
28 changes: 28 additions & 0 deletions facebook/delphiFacebook/unit-tests/testthat/test-variables.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@ library(testthat)

context("Testing response coding")

test_that("split_options splits correctly", {
expect_equal(split_options(c("1", "", "1,2")),
list(c("1"), character(0), c("1", "2")))

# Input logical vector
expect_equal(split_options(c(NA, NA, NA)),
list(NA_character_, NA_character_, NA_character_))

# Input character vector
expect_equal(split_options(c(NA_character_, NA_character_, NA_character_)),
list(NA_character_, NA_character_, NA_character_))

expect_equal(split_options(c("", NA_character_, NA)),
list(character(0), NA_character_, NA_character_))
})

test_that("is_selected handles selections correctly", {
expect_equal(is_selected(split_options(c("1", "", "1,2")), "1"),
c(TRUE, NA, TRUE))
Expand All @@ -18,6 +34,18 @@ test_that("is_selected handles selections correctly", {
expect_equal(is_selected(split_options(c("4,54", "3,6,2,54", "5,4,45")),
"54"),
c(TRUE, TRUE, FALSE))

expect_equal(is_selected(c(NA, NA, NA), "14"),
c(NA, NA, NA))

expect_equal(is_selected(c(NA_character_, NA_character_, NA_character_), "14"),
c(NA, NA, NA))

expect_equal(is_selected(list(NA, NA, NA), "14"),
c(NA, NA, NA))

expect_equal(is_selected(list(NA_character_, NA_character_, NA_character_), "14"),
c(NA, NA, NA))
})

test_that("activities items correctly coded", {
Expand Down