Skip to content

Commit 1ce3434

Browse files
committed
diff to export affected subqs columns for matrix items
1 parent 83c8b67 commit 1ce3434

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

facebook/qsf-tools/qsf-differ.R

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ get_qsf_file <- function(path,
5757
) {
5858
# Read file as json.
5959
qsf <- read_json(path)
60-
6160
## Block
6261
shown_items <- get_shown_items(qsf)
6362

@@ -136,8 +135,13 @@ diff_surveys <- function(old_qsf, new_qsf) {
136135
old_questions <- old_qsf$questions
137136
new_questions <- new_qsf$questions
138137

139-
added <- setdiff(new_shown_items, old_shown_items)
140-
removed <- setdiff(old_shown_items, new_shown_items)
138+
added_qs <- setdiff(new_shown_items, old_shown_items)
139+
added <- rep(NA, length(added_qs))
140+
names(added) <- added_qs
141+
142+
removed_qs <- setdiff(old_shown_items, new_shown_items)
143+
removed <- rep(NA, length(removed_qs))
144+
names(removed) <- removed_qs
141145

142146
added_df <- create_diff_df(added, "Added", NULL, new_questions)
143147
removed_df <- create_diff_df(removed, "Removed", old_questions, NULL)
@@ -171,13 +175,33 @@ diff_question <- function(names, change_type=c("Choices", "QuestionText",
171175
old_qsf, new_qsf) {
172176
change_type <- match.arg(change_type)
173177

174-
changed <- c()
178+
changed <- list()
175179
for (question in names) {
176180
if ( !identical(old_qsf[[question]][[change_type]], new_qsf[[question]][[change_type]]) ) {
177-
changed <- append(changed, question)
181+
changed_subquestions <- c()
182+
if (change_type == "Subquestions") {
183+
subquestion_codes <- unique(
184+
c(
185+
names(old_qsf[[question]][[change_type]]),
186+
names(new_qsf[[question]][[change_type]])
187+
)
188+
)
189+
190+
for (code in subquestion_codes) {
191+
if ( !identical(old_qsf[[question]][[change_type]][[code]], new_qsf[[question]][[change_type]][[code]]) ) {
192+
changed_subquestions <- append(changed_subquestions, code)
193+
}
194+
}
195+
changed_subquestions <- paste(changed_subquestions, collapse=", ")
196+
}
197+
198+
if (length(changed_subquestions) == 0) {
199+
changed_subquestions <- NA
200+
}
201+
changed[[question]] <- changed_subquestions
178202
}
179203
}
180-
out <- create_diff_df(changed, change_type, old_qsf, new_qsf)
204+
out <- create_diff_df(unlist(changed), change_type, old_qsf, new_qsf)
181205

182206
return(out)
183207
}
@@ -208,25 +232,26 @@ create_diff_df <- function(questions, change_type=c("Added", "Removed",
208232
Choices = "Answer choices changed",
209233
Subquestions = "Matrix subquestions changed"
210234
)
211-
questions <- sort(questions)
212235

213236
if (!is.null(old_qsf)) {
214-
old_qids <- sapply(questions, function(question) { old_qsf[[question]]$QuestionID })
237+
old_qids <- sapply(names(questions), function(question) { old_qsf[[question]]$QuestionID })
215238
} else {
216239
old_qids <- NA
217240
}
218241
if (!is.null(new_qsf)) {
219-
new_qids <- sapply(questions, function(question) { new_qsf[[question]]$QuestionID })
242+
new_qids <- sapply(names(questions), function(question) { new_qsf[[question]]$QuestionID })
220243
} else {
221244
new_qids <- NA
222245
}
223246

224247
out <- data.frame(
225248
change_type=change_descriptions[[change_type]],
226-
item=questions,
249+
item=names(questions),
227250
old_qid=old_qids,
228-
new_qid=new_qids
229-
)
251+
new_qid=new_qids,
252+
impacted_subquestions=questions
253+
) %>%
254+
arrange(item)
230255
}
231256

232257
return(out)

0 commit comments

Comments
 (0)