@@ -57,7 +57,6 @@ get_qsf_file <- function(path,
57
57
) {
58
58
# Read file as json.
59
59
qsf <- read_json(path )
60
-
61
60
# # Block
62
61
shown_items <- get_shown_items(qsf )
63
62
@@ -136,8 +135,13 @@ diff_surveys <- function(old_qsf, new_qsf) {
136
135
old_questions <- old_qsf $ questions
137
136
new_questions <- new_qsf $ questions
138
137
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
141
145
142
146
added_df <- create_diff_df(added , " Added" , NULL , new_questions )
143
147
removed_df <- create_diff_df(removed , " Removed" , old_questions , NULL )
@@ -171,13 +175,33 @@ diff_question <- function(names, change_type=c("Choices", "QuestionText",
171
175
old_qsf , new_qsf ) {
172
176
change_type <- match.arg(change_type )
173
177
174
- changed <- c ()
178
+ changed <- list ()
175
179
for (question in names ) {
176
180
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
178
202
}
179
203
}
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 )
181
205
182
206
return (out )
183
207
}
@@ -208,25 +232,26 @@ create_diff_df <- function(questions, change_type=c("Added", "Removed",
208
232
Choices = " Answer choices changed" ,
209
233
Subquestions = " Matrix subquestions changed"
210
234
)
211
- questions <- sort(questions )
212
235
213
236
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 })
215
238
} else {
216
239
old_qids <- NA
217
240
}
218
241
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 })
220
243
} else {
221
244
new_qids <- NA
222
245
}
223
246
224
247
out <- data.frame (
225
248
change_type = change_descriptions [[change_type ]],
226
- item = questions ,
249
+ item = names( questions ) ,
227
250
old_qid = old_qids ,
228
- new_qid = new_qids
229
- )
251
+ new_qid = new_qids ,
252
+ impacted_subquestions = questions
253
+ ) %> %
254
+ arrange(item )
230
255
}
231
256
232
257
return (out )
0 commit comments