This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
forked from Edouard-Legoupil/koboloadeR
/
kobo_edit_form.R
192 lines (176 loc) · 7.05 KB
/
kobo_edit_form.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
#' @name kobo_edit_form
#' @rdname kobo_edit_form
#' @title Edit XLS form with shiny app for configuration
#'
#' @description This function used to change the data of sheets in the xlsform and apply all required styles for each 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 folder.
#'
#' @param survey Dataframe that represent the data of survey sheet in the xlsform
#' @param choices Dataframe that represent the data of choices sheet in the xlsform
#' @param indicator Dataframe that represent the data of indicator sheet in the xlsform
#' @param settings Dataframe that represent the data of settings sheet in the xlsform
#' @param analysisSettings Dataframe that represent the data of analysisSettings sheet in the xlsform
#'
#' @return No return, this function edit the original XLSform directly
#'
#' @author Maher Daoud
#'
#'
#' @examples
#' \dontrun{
#' kobo_edit_form("form.xlsx")
#' }
#'
#' @export kobo_edit_form
#'
kobo_edit_form <- function(form = "form.xlsx", survey = NULL, choices = NULL, indicator = NULL, settings = NULL, analysisSettings=NULL) {
tryCatch({
wb <- openxlsx::createWorkbook()
mainDir <- kobo_getMainDirectory()
form_tmp <- paste(mainDir, "data", form, sep = "/", collapse = "/")
#################################### survey sheet ######################################
if(is.null(survey)){
survey <- tryCatch({
as.data.frame(readxl::read_excel(form_tmp, sheet = "survey"),
stringsAsFactors = FALSE) #read survey sheet from the form
}, error = function(err) {
data.frame( #if it doesn't exist, we need to create empty dataframe with those fields
type = character(),
name = character(),
label = character(),
variable = character(),
disaggregation = character(),
chapter = character(),
structuralequation.risk = character(),
structuralequation.coping = character(),
structuralequation.resilience = character(),
anonymise = character(),
correlate = character(),
clean = character(),
cluster = character(),
predict = character(),
mappoint = character(),
mappoly = character(),
stringsAsFactors = FALSE
)
})
}
if(!is.null(survey)){
survey[is.na(survey)] <- ""
sheetname <- "survey"
if(!is.null(openxlsx::getSheets(wb)[[sheetname]]))
openxlsx::removeSheet(wb, sheetname)
surveySheet <- openxlsx::createSheet(wb, sheetname) #create survey sheet in wb
openxlsx::addDataFrame(survey, surveySheet, col.names=TRUE, row.names=FALSE) #add survey dataframe in the survey sheet
}
#################################### choices sheet ######################################
if(is.null(choices)){
choices <- tryCatch({
as.data.frame(readxl::read_excel(form_tmp, sheet = "choices"),
stringsAsFactors = FALSE) #read survey sheet from the form
}, error = function(err) {
data.frame( #if it doesn't exist, we need to create empty dataframe with those fields
list_name = character(),
name = character(),
label = character(),
order = character(),
stringsAsFactors = FALSE
)
})
}
if(!is.null(choices)){
sheetname <- "choices"
if(!is.null(openxlsx::getSheets(wb)[[sheetname]]))
openxlsx::removeSheet(wb, sheetname)
choicesSheet <- openxlsx::createSheet(wb, sheetName=sheetname)
openxlsx::addDataFrame(choices, choicesSheet, col.names=TRUE, row.names=FALSE)
}
#################################### indicator sheet ######################################
if(is.null(indicator)){
indicator <- tryCatch({
as.data.frame(readxl::read_excel(form_tmp, sheet = "indicator"),stringsAsFactors = FALSE)
}, error = function(err) {
data.frame(
type = character(),
fullname = character(),
labelReport = character(),
hintReport = character(),
frame = character(),
listname = character(),
calculation = character(),
chapter = character(),
disaggregation = character(),
correlate = character(),
anonymise = character(),
cluster = character(),
predict = character(),
variable = character(),
mappoint = character(),
mappoly = character(),
structuralequation.risk = character(),
structuralequation.coping = character(),
structuralequation.resilience = character(),
stringsAsFactors = FALSE
)
})
}
if(!is.null(indicator)){
sheetname <- "indicator"
if(!is.null(openxlsx::getSheets(wb)[[sheetname]]))
openxlsx::removeSheet(wb, sheetname)
indicatorSheet <- openxlsx::createSheet(wb, sheetName=sheetname)
openxlsx::addDataFrame(indicator, indicatorSheet, col.names=TRUE, row.names=FALSE)
}
#################################### settings sheet ######################################
if(is.null(settings)){
settings <- tryCatch({
as.data.frame(readxl::read_excel(form_tmp, sheet = "settings"),
stringsAsFactors = FALSE)
}, error = function(err) {
data.frame(
form_title = character(),
form_id = character(),
default_language = character(),
stringsAsFactors = FALSE
)
})
}
if(!is.null(settings)){
sheetname <- "settings"
if(!is.null(openxlsx::getSheets(wb)[[sheetname]]))
openxlsx::removeSheet(wb, sheetname)
settingsSheet <- openxlsx::createSheet(wb, sheetName=sheetname) #create sheet with settings name
openxlsx::addDataFrame(settings, settingsSheet, col.names=TRUE, row.names=FALSE) #add settings data frame to this sheet
}
#################################### settings sheet ######################################
if(is.null(analysisSettings)){
analysisSettings <- tryCatch({
as.data.frame(readxl::read_excel(form_tmp, sheet = "analysisSettings"),
stringsAsFactors = FALSE)
}, error = function(err) {
data.frame(
name = character(),
label = character(),
value = character(),
path = character(),
stringsAsFactors = FALSE
)
})
}
if(!is.null(analysisSettings)){
sheetname <- "analysisSettings"
if(!is.null(openxlsx::getSheets(wb)[[sheetname]]))
openxlsx::removeSheet(wb, sheetname)
settingsSheet <- openxlsx::createSheet(wb, sheetName=sheetname) #create sheet with analysisSettings name
openxlsx::addDataFrame(analysisSettings, settingsSheet, col.names=TRUE, row.names=FALSE) #add analysisSettings data frame to this sheet
}
if (file.exists(form_tmp)) file.remove(form_tmp)
openxlsx::saveWorkbook(wb, form_tmp)
}, error = function(err) {
print("kobo_load_data_ERROR")
return(structure(err, class = "try-error"))
})
}
NULL