Skip to content

Commit aeb22f9

Browse files
committed
treat dropdown like matrix; null long response options
1 parent 2bc9462 commit aeb22f9

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

facebook/qsf-tools/generate-codebook.R

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ process_qsf <- function(path_to_qsf,
2828
path_to_rename_map <- localize_static_filepath(rename_map_file, survey_version)
2929
path_to_drop_columns <- localize_static_filepath(drop_columns_file, survey_version)
3030

31-
q <- read_json(path_to_qsf)
31+
q <- read_json(path_to_qsf, encoding = "UTF-8")
3232
wave <- get_wave(path_to_qsf)
3333

3434
displayed_questions <- subset_qsf_to_displayed(q)
@@ -108,7 +108,7 @@ process_qsf <- function(path_to_qsf,
108108
}
109109
)
110110

111-
# get the "answers". These are answer choices for matrix items, and missing for non-matrix items.
111+
# get the "answers". These are answer choices for matrix and dropdown items, and missing for other questions.
112112
matrix_answers <- displayed_questions %>%
113113
map(~ .x$Payload$Answers) %>%
114114
map(~ map(.x, "Display"))
@@ -123,7 +123,7 @@ process_qsf <- function(path_to_qsf,
123123
}) %>% unlist() %>% which()
124124

125125
# Swap matrix answer choices into `choices` and matrix subquestion text into another variable
126-
ii_matrix <- which(qtype == "Matrix")
126+
ii_matrix <- which(qtype == "Matrix" | qtype == "Dropdown")
127127
matrix_subquestions <- rep(list(list()), length(choices))
128128
matrix_subquestions[ii_matrix] <- choices[ii_matrix]
129129
choices[ii_matrix] <- matrix_answers[ii_matrix]
@@ -142,14 +142,18 @@ process_qsf <- function(path_to_qsf,
142142
map(~ .x$Payload$Answers) %>%
143143
map(~ map(.x, ~ map(.x, "Display")))
144144

145+
# Null out dropdown choices to avoid including super long list.
146+
ii_dropdown <- which(qtype == "Dropdown")
147+
choices[ii_dropdown] <- rep(list("Response options not listed due to length. Please see additional documentation"), length(ii_dropdown))
148+
145149
# Get matrix subquestion field names as reported in microdata. NULL if not
146150
# defined (not a Matrix question); FALSE if not set; otherwise a list
147151
matrix_subquestion_field_names <- displayed_questions %>%
148152
map(~ .x$Payload$ChoiceDataExportTags)
149153
# When subquestion field names are not set, generate incrementing names
150154
ii_unset_matrix_subq_names <- (matrix_subquestion_field_names %>%
151155
map(~ !inherits(.x, "list")) %>%
152-
unlist() & qtype == "Matrix") %>%
156+
unlist() & (qtype == "Matrix" | qtype == "Dropdown")) %>%
153157
which()
154158
matrix_subquestion_field_names[ii_unset_matrix_subq_names] <- lapply(ii_unset_matrix_subq_names, function(ind){
155159
paste(
@@ -331,18 +335,18 @@ process_qsf <- function(path_to_qsf,
331335

332336
# separate matrix subquestions into separate fields (to match exported data)
333337
nonmatrix_items <- qdf %>%
334-
filter(question_type != "Matrix") %>%
338+
filter(question_type != "Matrix" & question_type != "Dropdown") %>%
335339
mutate(matrix_base_name = NA_character_) %>%
336340
select(-matrix_subquestion_field_names)
337341

338342
has_response_by_subq <- qdf %>%
339-
filter(question_type == "Matrix") %>%
343+
filter(question_type == "Matrix" | question_type == "Dropdown") %>%
340344
pull(response_options) %>%
341345
map_lgl(~ all(map_lgl(.x, ~ inherits(.x, "list"))) &&
342346
!identical(.x, list()))
343347

344348
matrix_items <- qdf %>%
345-
filter(question_type == "Matrix") %>%
349+
filter(question_type == "Matrix" | question_type == "Dropdown") %>%
346350
filter(!has_response_by_subq) %>%
347351
rowwise() %>%
348352
mutate(new = list(
@@ -364,7 +368,7 @@ process_qsf <- function(path_to_qsf,
364368

365369

366370
matrix_items_resp_by_subq <- qdf %>%
367-
filter(question_type == "Matrix") %>%
371+
filter(question_type == "Matrix" | question_type == "Dropdown") %>%
368372
filter(has_response_by_subq) %>%
369373
rowwise() %>%
370374
mutate(new = list(
@@ -395,8 +399,6 @@ process_qsf <- function(path_to_qsf,
395399
mutate(variable = if_else(str_starts(variable, "C10"), paste0(variable, "_1"), variable),
396400
question_type = if_else(str_starts(variable, "A5|C10"), "Text", question_type),
397401
response_options = if_else(str_starts(variable, "A5|C10"), list(list()), response_options))
398-
} else if (survey_version == "UMD") {
399-
# pass
400402
}
401403

402404
qdf <- bind_rows(nonmatrix_items, matrix_items)

0 commit comments

Comments
 (0)