forked from mrdwab/koboloadeR
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathkobo_create_indicators.R
457 lines (349 loc) · 21.8 KB
/
kobo_create_indicators.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#' @name kobo_create_indicators
#' @rdname kobo_create_indicators
#' @title Create Indicators
#'
#' @description Function to compute indicators from indicator sheet
#'
#' @param form The full filename of the form to be accessed (xls or xlsx file).
#' It is assumed that the form is stored in the data-raw folder.
#'
#'
#' @return No return, all results will be saved inside new CSV files
#'
#' @author Edouard Legoupil, Maher Daoud
#'
#'
#' @examples
#' \dontrun{
#' kobo_create_indicators("myform.xlsx")
#' }
#'
#' @export kobo_create_indicators
#'
kobo_create_indicators <- function(form = "form.xlsx") {
mainDir <- koboloadeR::kobo_getMainDirectory()
form_tmp <- paste(mainDir, "data-raw", form, sep = "/", collapse = "/")
tryCatch({
#### Load and test i indicators #############################################################################
#library(readxl)
tried <- try(readxl::read_excel(form_tmp, sheet = "indicator"),
silent = TRUE)
if (inherits(tried, "try-error")) {
writeLines("Note that you have not defined (or defined correctly) indicators within your xlsform file. \n")
} else {
rm(tried)
## load all required data files #########################################
cat("\n\nload all required data files..\n")
dataBeginRepeat <- koboloadeR::kobo_get_begin_repeat(form)
dataBeginRepeat <- dataBeginRepeat$names
## Check if there's a repeat - aka hierarchical structure in the dataset
if (length(dataBeginRepeat) > 0) {
for (dbr in dataBeginRepeat) {
dataFrame <- utils::read.csv(paste(mainDir,"/data/",dbr,"_edited.csv",sep = ""),stringsAsFactors = F)
#load(paste(mainDir,"/data/",dbr,"_edited.rda",sep = ""))
assign(dbr, dataFrame)
}
}
indicator <- readxl::read_excel(form_tmp, sheet = "indicator")
if (nrow(indicator) == 0) {
writeLines("Note that you have not defined (or defined correctly) indicators within your xlsform file. \n")
} else {
## Load data & dico #############################################################################
#form <- "form.xls"
## Run this only after data cleaning
dico <- utils::read.csv(paste0(mainDir,"/data/dico_",form,".csv"), encoding = "UTF-8", na.strings = "")
#load(paste(mainDir,"/data/dico_",form,".rda",sep = ""))
## Create the dicotemp #############################################################################
#names(dico)
dicotemp <- data.frame(c("trigger"))
names(dicotemp)[1] <- "type"
#dicotemp$type <- "trigger"
dicotemp$name <- "trigger"
dicotemp$fullname <- "trigger"
dicotemp$label <- "trigger"
dicotemp$labelReport <- "trigger"
dicotemp$hintReport <- "trigger"
dicotemp$report <- "trigger"
dicotemp$chapter <- "trigger"
dicotemp$disaggregation <- "trigger"
dicotemp$correlate <- "trigger"
dicotemp$anonymise <- "trigger"
dicotemp$clean <- "trigger"
dicotemp$cluster <- "trigger"
dicotemp$predict <- "trigger"
dicotemp$variable <- "trigger"
dicotemp$mappoint <- "trigger"
dicotemp$mappoly <- "trigger"
dicotemp$listname <- "trigger"
dicotemp$qrepeat <- "trigger"
dicotemp$qrepeatlabel <- "trigger"
dicotemp$qlevel <- "trigger"
dicotemp$qgroup <- "trigger"
dicotemp$labelchoice <- "trigger"
dicotemp$variable <- "trigger"
dicotemp$order <- "trigger"
dicotemp$weight <- "trigger"
dicotemp$score <- "trigger"
dicotemp$recategorise <- "trigger"
dicotemp$formpart <- "trigger"
dicotemp$indic <- "trigger"
dicotemp$constraint <- "trigger"
dicotemp$label <- "trigger"
dicotemp$relevant <- "trigger"
dicotemp$repeat_count <- "trigger"
dicotemp$required <- "trigger"
## Need to check that all column are presents...
## Load indicator info #############################################################################
if (file.exists(paste0(mainDir,"/R/build_indicator.R") )) file.remove(paste0(mainDir,"/R/build_indicator.R"))
cat(paste('### Script to generate indicator: ',sep = ""), file = paste0(mainDir,"/R/build_indicator.R") , sep = "\n", append = TRUE)
cat(paste('form <- "',form,'"',sep = ""), file = paste0(mainDir,"/R/build_indicator.R") , sep = "\n", append = TRUE)
cat("mainDir <- koboloadeR::kobo_getMainDirectory()", file = paste0(mainDir,"/R/build_indicator.R") , sep = "\n", append = TRUE)
cat('form_tmp <- paste(mainDir, "data", form, sep = "/", collapse = "/")', file = paste0(mainDir,"/R/build_indicator.R") , sep = "\n", append = TRUE)
cat('dataBeginRepeat <- koboloadeR::kobo_get_begin_repeat(form)', file = paste0(mainDir,"/R/build_indicator.R") , sep = "\n", append = TRUE)
cat('dataBeginRepeat <- dataBeginRepeat$names', file = paste0(mainDir,"/R/build_indicator.R") , sep = "\n", append = TRUE)
cat('MainDataFrame <- utils::read.csv(paste(mainDir,"/data/MainDataFrame_edited.csv",sep = ""), encoding = "UTF-8", na.strings = "NA")', file = paste0(mainDir,"/R/build_indicator.R") , sep = "\n", append = TRUE)
#cat('load(paste(mainDir,"/data/MainDataFrame_edited.rda",sep = ""))', file = paste0(mainDir,"/R/build_indicator.R") , sep = "\n", append = TRUE)
## Check if there's a repeat - aka hierarchical structure in the dataset
if (length(dataBeginRepeat) > 0) {
cat('for (dbr in dataBeginRepeat) {
dataFrame <- utils::read.csv(paste(mainDir,"/data/",dbr,"_edited.csv",sep = ""),stringsAsFactors = F)
#load(paste(mainDir,"/data/",dbr,"_edited.rda",sep = ""))
assign(paste0(dbr,"_edited"), dataFrame) }', file = paste0(mainDir,"/R/build_indicator.R") , sep = "\n", append = TRUE)
}
for (i in 1:nrow(indicator))
{
# i <- 1
indicator.type <- as.character(indicator[ i, c("type")])
indicator.fullname <- as.character(indicator[ i, c("fullname")])
# indicator.label <- as.character(indicator[ i, c("label")])
indicator.label <- as.character("")
indicator.labelReport <- as.character(indicator[ i, c("labelReport")])
indicator.hintReport <- as.character(indicator[ i, c("hintReport")])
indicator.report <- as.character(indicator[ i, c("report")])
indicator.chapter <- as.character(indicator[ i, c("chapter")])
indicator.disaggregation <- as.character(indicator[ i, c("disaggregation")])
indicator.correlate <- as.character(indicator[ i, c("correlate")])
indicator.anonymise <- as.character(indicator[ i, c("anonymise")])
indicator.frame <- as.character(indicator[ i, c("frame")])
indicator.listname <- as.character(indicator[ i, c("listname")])
indicator.calculation <- as.character(indicator[ i, c("calculation")])
indicator.cluster <- as.character(indicator[ i, c("cluster")])
indicator.predict <- as.character(indicator[ i, c("predict")])
indicator.variable <- as.character(indicator[ i, c("variable")])
indicator.mappoint <- as.character(indicator[ i, c("mappoint")])
indicator.mappoly <- as.character(indicator[ i, c("mappoly")])
cat(paste0(i, "- Load indicator: ", indicator.labelReport," of type: ",indicator.type,"\n"))
## Build and run the formula to insert the indicator in the right frame ###########################
indic.formula <- paste0(indicator.frame,"$",indicator.fullname," <- ",indicator.calculation )
cat(paste('\n \n### Script to generate indicator: ',indicator.labelReport,sep = ""), file = paste0(mainDir,"/R/build_indicator.R") , sep = "\n", append = TRUE)
cat(indic.formula, file = paste0(mainDir,"/R/build_indicator.R") , sep = "\n", append = TRUE)
## do a check on indicator variable type
indicator.type2 <- indicator.type
ifelse(indicator.type == "select_one", indicator.type2 <- "character", indicator.type2 <- indicator.type)
cat(paste0(indicator.frame,"$",indicator.fullname," <- as.",indicator.type2,"(",indicator.frame,"$",indicator.fullname,")"), file = paste0(mainDir,"/R/build_indicator.R") , sep = "\n", append = TRUE)
cat(paste0("str(",indicator.frame,"$",indicator.fullname,")"), file = paste0(mainDir,"/R/build_indicator.R") , sep = "\n", append = TRUE)
cat(paste0("summary(",indicator.frame,"$",indicator.fullname,")"), file = paste0(mainDir,"/R/build_indicator.R") , sep = "\n", append = TRUE)
# if (indicator.frame == "MainDataFrame_edited") {
# }else{
# cat(paste('dbr<-"',indicator.frame,'"',sep = ""))
# cat('utils::write.csv(eval(as.name(dbr)),paste(mainDir,"/data/",dbr,".csv",sep = ""), row.names = FALSE, na = "")', file = paste0(mainDir,"/R/build_indicator.R") , sep = "\n", append = TRUE)
# }
# source(paste0(mainDir,"/R/build_indicator.R"))
# cat(paste0(i, "- Executed indicator: ", indicator.labelReport,"\n"))
# if (file.exists(paste0(mainDir,"/R/build_indicator.R"))) file.remove(paste0(mainDir,"/R/build_indicator.R"))
## Insert the indicator in a temp dico frame to be appended to the full dico ######################
dicotemp1 <- data.frame(c("trigger"))
names(dicotemp1)[1] <- "type"
dicotemp1$type <- indicator.type
dicotemp1$name <- indicator.fullname
dicotemp1$fullname <- indicator.fullname
dicotemp1$label <- indicator.label
dicotemp1$labelReport <- indicator.labelReport
dicotemp1$hintReport <- indicator.hintReport
dicotemp1$report <- indicator.report
dicotemp1$chapter <- indicator.chapter
dicotemp1$disaggregation <- indicator.disaggregation
dicotemp1$correlate <- indicator.correlate
dicotemp1$anonymise <- indicator.anonymise
dicotemp1$clean <- " "
dicotemp1$cluster <- indicator.cluster
dicotemp1$predict <- indicator.predict
dicotemp1$variable <- indicator.variable
dicotemp1$mappoint <- indicator.mappoint
dicotemp1$mappoly <- indicator.mappoly
dicotemp1$listname <- indicator.listname
dicotemp1$qrepeat <- " "
dicotemp1$qrepeatlabel <- indicator.frame
dicotemp1$qlevel <- " "
dicotemp1$qgroup <- " "
dicotemp1$labelchoice <- " "
dicotemp1$order <- " "
dicotemp1$weight <- " "
dicotemp1$score <- " "
dicotemp1$recategorise <- " "
dicotemp1$formpart <- " "
dicotemp1$indic <- "feature"
dicotemp1$constraint <- " "
dicotemp1$label <- " "
dicotemp1$relevant <- " "
dicotemp1$repeat_count <- " "
dicotemp1$required <- " "
dicotemp <- rbind(dicotemp,dicotemp1)
}
cat('utils::write.csv(MainDataFrame, paste(mainDir,"/data/MainDataFrame_edited.csv",sep = ""), row.names = FALSE, na = "")', file = paste0(mainDir,"/R/build_indicator.R") , sep = "\n", append = TRUE)
#cat('save(MainDataFrame, file = paste(mainDir,"/data/MainDataFrame_edited.rda",sep = ""))', file = paste0(mainDir,"/R/build_indicator.R") , sep = "\n", append = TRUE)
source(paste0(mainDir,"/R/build_indicator.R"))
## Append indicators in the dico #############################################################################
## removing first line
dicotemp <- dicotemp[ 2:nrow(dicotemp), ]
### Merging choices from the newly created indicators #################################################################
cat("\n\n\n It's assumed that the modalities for newly calculated categoric indicators are in the same xlsform - choices worksheet \n\n\n\n")
choices <- readxl::read_excel(form_tmp, sheet = "choices")
#rm(choices)
#names(choices)[names(choices) == "labelReport"] <- "label"
names(choices)[names(choices) == "label::english"] <- "label"
names(choices)[names(choices) == "label::English"] <- "label"
names(choices)[names(choices) == "label::English (en)"] <- "label"
names(choices)[names(choices) == "list name"] <- "listname"
names(choices)[names(choices) == "list_name"] <- "listname"
## Remove trailing space
choices$listname <- trimws(choices$listname)
choices$label <- trimws(choices$label)
if ("order" %in% colnames(choices))
{
cat(" Good: You have a column `order` in your `choices` worksheet.\n");
} else
{cat(" No column `order` in your `choices` worksheet. Creating a dummy one for the moment...\n");
choices$order <- ""}
if ("weight" %in% colnames(choices))
{
cat(" Good: You have a column `weight` in your `choices` worksheet.\n");
} else
{cat(" No column `weight` in your `choices` worksheet. Creating a dummy one for the moment...\n");
choices$weight <- ""}
if ("recategorise" %in% colnames(choices))
{
cat(" Good: You have a column `recategorise` in your `choices` worksheet.\n");
} else
{cat(" No column `recategorise` in your `choices` worksheet. Creating a dummy one for the moment...\n");
choices$recategorise <- ""}
if ("score" %in% colnames(choices))
{
cat(" Good: You have a column `score` in your `choices` worksheet.\n");
} else
{
cat(" No column `score` in your `choices` worksheet. Creating a dummy one for the moment...\n");
choices$score <- ""
}
choices <- choices[,c("listname", "name", "labelReport", "order", "weight","score","recategorise")]
names(choices)[names(choices) == "labelReport"] <- "labelchoice"
# names(choices)[names(choices) == "label"] <- "labelchoice"
#rm(choices)
dicotemp.choice <- dicotemp[ !(is.na(dicotemp$listname)), c( "type", "name",
"fullname","labelReport","hintReport","report","chapter",
"disaggregation","correlate","anonymise",
# "structuralequation.risk","structuralequation.coping","structuralequation.resilience",
"clean","cluster","predict","variable","mappoint","mappoly",
"listname","qrepeat","qrepeatlabel","qlevel","qgroup",
# "order","weight","score","recategorise",
"formpart","indic","constraint","label","relevant","repeat_count","required" )]
choices2 <- plyr::join(x = dicotemp.choice, y = choices, by = "listname", type = "left")
choices2$type <- with(choices2, ifelse(grepl("select_one", ignore.case = TRUE, fixed = FALSE, useBytes = FALSE, choices2$type),
paste0("select_one_d"),choices2$type))
choices2$type <- with(choices2, ifelse(grepl("select_multiple_d", ignore.case = TRUE, fixed = FALSE, useBytes = FALSE, choices2$type),
paste0("select_multiple"),choices2$type))
names(choices2)[2] <- "nameq"
names(choices2)[3] <- "nameqfull"
names(choices2)[4] <- "labelq"
# choices$labelchoice
# View(choices2[ , c("labelchoice")])
#choices2$labelchoice <- as.factor(choices2$labelchoice)
#str(choices2$labelchoice)
#names(choices2)
# choices2$labelq
choices2$labelfull <- paste(choices2$labelq, choices2$labelchoice, sep = ": ")
choices2$namefull <- paste(choices2$nameqfull, choices2$name, sep = ".")
# names(choices2)
#### Now Row bind questions & choices########################################################################################################
choices3 <- choices2[ ,c("type", "name", "namefull", "labelfull","hintReport",
"report", "chapter", "disaggregation","correlate", "anonymise",
# "structuralequation.risk","structuralequation.coping","structuralequation.resilience",
"clean", "cluster", "predict",
"variable", "mappoint", "mappoly", "listname",
"qrepeat", "qrepeatlabel","qlevel","qgroup",
"labelchoice", "order", "weight","score",
"recategorise",
"formpart","indic","constraint","label","relevant","repeat_count","required")]
names(choices3)[names(choices3) == "namefull"] <- "fullname"
names(choices3)[names(choices3) == "labelfull"] <- "labelReport"
#names(choices3)[names(choices3) == "labelfull"] <- "label"
dicotemp <- dicotemp[,c( "type", "name", "fullname", #"label",
"labelReport","hintReport",
"report", "chapter", "disaggregation","correlate", "anonymise",
#"structuralequation.risk","structuralequation.coping","structuralequation.resilience",
"clean", "cluster", "predict",
"variable", "mappoint", "mappoly", "listname",
"qrepeat", "qrepeatlabel","qlevel","qgroup",
"labelchoice", "order", "weight","score",
"recategorise",
"formpart","indic","constraint","label","relevant","repeat_count","required")]
### Check -- normally there should not be duplicate
dicotemp$formpart <- "questions"
choices3$formpart <- "answers"
# test1 <- as.data.frame(names(dicotemp))
# test2 <- as.data.frame(names(choices3))
dicotemp <- rbind(dicotemp,choices3)
dicotemp$label <- dicotemp$labelReport
dicotemp$indic <- "feature"
dico$indic <- "data"
dicotemp$relevant <- ""
dicotemp$required <- ""
dicotemp$constraint <- ""
dicotemp$repeat_count <- ""
#names(dico)
#names(dicotemp)
dico <- dico[ , c( "type", "name", "fullname", "label", "labelReport","hintReport",
"report","chapter", "disaggregation","correlate", "anonymise",
#"structuralequation.risk","structuralequation.coping","structuralequation.resilience",
"anonymise", "clean", "cluster", "predict", "variable", "mappoint", "mappoly",
"relevant", "required", "constraint", "repeat_count",
"listname","qrepeat", "qrepeatlabel","qlevel","qgroup",
"labelchoice", "order", "weight","score",
"recategorise", "formpart", "indic" )]
dicotemp <- dicotemp[ , c("type", "name", "fullname", "label", "labelReport","hintReport",
"report", "chapter", "disaggregation","correlate", "anonymise",
# "structuralequation.risk","structuralequation.coping","structuralequation.resilience",
"anonymise", "clean", "cluster", "predict", "variable", "mappoint", "mappoly",
"relevant", "required", "constraint", "repeat_count",
"listname","qrepeat", "qrepeatlabel","qlevel","qgroup",
"labelchoice", "order", "weight","score",
"recategorise", "formpart", "indic" )]
dico <- rbind(dico,dicotemp)
rm(dicotemp,dicotemp1, choices, choices2, choices3, dicotemp.choice)
MainDataFrame <- utils::read.csv(paste(mainDir,"/data/MainDataFrame_edited.csv",sep = ""), encoding = "UTF-8", na.strings = "NA")
#load(paste(mainDir,"/data/MainDataFrame_edited.rda",sep = ""))
## label Variables
cat("\n\n quick check on labeling\n")
MainDataFrame <- koboloadeR::kobo_label(MainDataFrame , dico)
## Check if there's a repeat - aka hierarchical structure in the dataset
if (length(dataBeginRepeat) > 0) {
for (dbr in dataBeginRepeat) {
dataFrame <- utils::read.csv(paste(mainDir,"/data/",dbr,"_edited.csv",sep = ""),stringsAsFactors = F)
#load(paste(mainDir,"/data/",dbr,"_edited.rda",sep = ""))
dataFrame <- koboloadeR::kobo_label(dataFrame, dico)
utils::write.csv(dataFrame,paste(mainDir,"/data/",dbr,"_edited.csv",sep = ""), row.names = FALSE, na = "")
#save(dataFrame , file = paste(mainDir,"/data/",dbr,"_edited.rda",sep = ""))
}
}
cat("\n\nWrite dico\n")
utils::write.csv(dico, paste0(mainDir,"/data/dico_",form,".csv"), row.names = FALSE, na = "")
# save(dico, file = paste0(mainDir,"/data/dico_",form,".rda"))
utils::write.csv(MainDataFrame, paste(mainDir,"/data/MainDataFrame_edited.csv",sep = ""), row.names = FALSE, na = "")
#save(MainDataFrame, file = paste(mainDir,"/data/MainDataFrame_edited.rda",sep = ""))
}
}
}, error = function(err) {
print("There was an error in the indicator creation step!!! \n\n")
return(structure(err, class = "try-error"))
})
}