-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreport2_compact.Rmd
300 lines (245 loc) · 8.04 KB
/
report2_compact.Rmd
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
---
author: ""
date: "`r format(Sys.time(), '%d %B, %Y')`"
output: word_document
params:
file:
value: NA
---
```{r setup, include = FALSE}
# Packages needed ----
library(readxl)
library(stringr)
library(flextable)
library(ftExtra)
library(tidyverse)
library(knitr)
library(rmarkdown)
# Setting the flextable defaults -> this formats the tables
set_flextable_defaults(font.family = "Calibri",
border.color = "black",
font.size = 9,
theme_fun = "theme_vanilla",
big.mark = ",",
table.layout = "autofit")
# Loading the form(s) i.e:
# 1) Questions (For questions - keeps the markdown formatting)
# 2) Choices (For choices)
# 3) Settings (For the Document title via the 'Settings' sheet)
survey_questions <- read_excel(params$file, sheet = "survey")
survey_choices <- read_excel(params$file, sheet = "choices")
title <- read_excel(params$file, sheet = "settings")
# Title is determined by the FORM TITLE in the SETTINGS sheet
title <- title$form_title
# dropping blanks
survey_choices <- survey_choices |>
filter(!is.na(list_name))
survey_questions <- survey_questions |>
filter(!is.na(label))
# removing extra space from between the question and the list_name
survey_questions <-
survey_questions |>
mutate(type = gsub(pattern = "\\s+",
replacement = " ",
x = type))
# if numeric, we add square brackets around the value, if not, leave as is.
survey_choices <- survey_choices |>
mutate(name = paste("[", name, "]",
sep = "")) |>
mutate(label = paste(name, label,
sep = " "))
survey_choices <- survey_choices |>
group_by(list_name) |>
mutate(new_list = paste(label,
collapse="\n")) |>
group_by(list_name) |>
mutate(id = row_number()) |>
mutate(new_list = ifelse(test = id == 1,
yes = new_list,
no = NA)) |>
subset(!is.na(new_list))
## Splitting the question column and stripping all the HTML code ----
survey_questions <-
survey_questions |>
separate(col = type,
into = c("type",
"list_name"),
sep = " ") |>
mutate(label = gsub("#", "", label)) |>
mutate(label = gsub("@", "[at]", label)) |>
mutate(label = gsub("<(.|\n)*?>", "", label)) |>
mutate(label = gsub("\\*", "", label))
groups <-
survey_questions |>
subset(type == "begin_group") |>
mutate(list_name = name) |>
select(list_name)
group_names <- as.vector(groups$list_name)
# all question types that dont require a list per say
types <- c("integer",
"decimal",
"range",
"text",
"rank ",
"note",
"geopoint",
"geotrace",
"geoshape",
"date",
"time",
"dateTime",
"image",
"audio",
"background-audio",
"video",
"file",
"barcode",
"calculate",
"acknowledge",
"hidden",
"xml-external"
)
survey_questions$list_name <-
ifelse(
survey_questions$type %in% types,
yes = survey_questions$type,
no = survey_questions$list_name)
survey_choices <- as.data.frame(survey_choices)
# adding these to the choice sheet
survey_choices <- survey_choices %>%
add_row(
list_name = c("integer",
"decimal",
"range",
"text",
"note",
"geopoint",
"geotrace",
"geoshape",
"date",
"time",
"dateTime",
"image",
"audio",
"background-audio",
"video",
"file",
"barcode",
"calculate",
"acknowledge",
"hidden",
"xml-external"),
label = c("[Integer]",
"[Decimal]",
"[Range]",
"[Text]",
"[Enumerator Note]",
"[Lat, Long, Alt]",
"[Lat, Long, Alt]",
"[Lat, Long, Alt]",
"[Date]",
"[Time]",
"[Date + Time]",
"[Image]",
"[Audio]",
"[Background Audio]",
"[Video]",
"[File]",
"[Barcode/QR Code]",
"[Calculation]",
"[Acknowledge]",
"[Hidden]",
"[XML External]"
)
)
survey_choices <- survey_choices %>%
add_row(
list_name = c(group_names))
survey_questions <- survey_questions |>
mutate(list_name = ifelse(test = type == "begin_group",
yes = name,
no = list_name))
## Merging choices & questions via the choice label ----
survey_questions <- survey_questions |>
inner_join(x = survey_questions,
y = survey_choices,
by = "list_name")
survey_questions <- survey_questions |>
mutate(label.y = ifelse(test = type == "begin_group",
yes = label.x,
no = label.y))
survey_questions <-
survey_questions |>
mutate(new_list = ifelse(test = is.na(new_list),
yes = label.y,
no = new_list))
# some more cleaning for the forms
survey_questions <- survey_questions |>
group_by(name.x) |>
mutate(id = row_number()) |>
mutate(label.x = ifelse(test = id == 1,
yes = label.x,
no = NA))
survey_questions <- survey_questions|>
select(name.x,
label.x,
new_list) |>
rename(Variable = name.x)
# need to correct column labels
survey_questions <- survey_questions|>
rename(Question = label.x,
Choices = new_list) |>
mutate(Question = replace_na(Question, " ")) |>
select(Variable,
Question,
Choices)
# final table to be printed
final <- subset(survey_questions,
select = c("Variable",
"Question",
"Choices"))
final <- final |>
mutate(test_section = str_detect(Choices, "\\[", negate = TRUE)) |>
mutate(Choices = ifelse(test = test_section == TRUE,
yes = NA,
no = Choices)) |>
mutate(Variable = ifelse(test = test_section == TRUE,
yes = "Section Name",
no = Variable)) |>
mutate(Choices = replace_na(Choices, "")) |>
select(-test_section)
rm(survey_choices, survey_questions)
```
---
title: "`r title`"
---
```{r ft.split = TRUE, echo = F}
final <-
final |>
flextable() |>
set_caption(caption = "Questionnaire",
style = "Table Caption")
# formatting the table
final <- bg(final,
bg = "white",
part = "header")
final <- bg(final,
i = ~ Variable == "Section Name",
bg = "#345A8A",
part = "body")
final <- color(final,
i = ~ Variable == "Section Name",
color = "white",
part = "body")
final <- bold(final,
~ !is.na(Question),
c(1:2))
final <- italic(final,
~ !is.na(Question), 1)
final <- color(final,
~ !is.na(Question) & Variable!="Section Name",
1,
part = "body",
color = "blue")
final
```