-
Notifications
You must be signed in to change notification settings - Fork 0
/
exam.R
468 lines (443 loc) · 13.3 KB
/
exam.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
458
459
460
461
462
463
464
465
466
467
468
#' `exam` S3 class
#'
#' Creates an `exam` object.
#'
#' A test is based on an Rmd template that has predefined parameters whose values
#' are filled in using the functions of this object. In the `rmd` parameter, we
#' specify the template file.
#'
#' From the template, we generate multiple instances of the exam. We can specify
#' the instances to generate in two ways: by indicating a vector of examinee names
#' (using the `examinees` parameter) or by specifying the number of instances to
#' generate (using the `instances_num` parameter). If both are indicated, the
#' examinee names take precedence.
#'
#' We can generate the instances either randomly or sequentially, depending on the
#' instance number we generate. This is controlled by the `random` parameter.
#'
#' Additionally, in each test, we can include the exercises in the same order as
#' they are defined or in random order. This is indicated by the `reorder_exercises`
#' parameter.
#'
#' Finally, using the `select_n_exercises` parameter, we can specify the number
#' of exercises to include in each test. From all available exercises, the quantity
#' specified in this parameter will be randomly selected. By default, all defined
#' exercises are included.
#'
#' @param rmd A string representing the path to the Rmd file, the exam template.
#' @param examinees A vector of strings, representing the names of instances to
#' generate.
#' @param instances_num An integer, representing the number of instances to generate
#' if the examinee names are not provided.
#' @param random A boolean, indicating whether to generate instances randomly or
#' sequentially.
#' @param reorder_exercises A boolean, indicating whether to reorder exercises in
#' the exam.
#' @param select_n_exercises An integer, representing the number of exercises to
#' include.
#' @return An `exam` object.
#'
#' @family exam definition
#' @seealso \code{\link{define_an_exercise}}
#'
#' @examples
#'
#' rmd <- system.file("extdata/template01.Rmd", package = "rexer")
#' ex <- exam(
#' rmd = rmd,
#' examinees = NULL,
#' instances_num = 10,
#' random = TRUE,
#' reorder_exercises = TRUE,
#' select_n_exercises = NULL
#' )
#'
#' @export
exam <-
function(rmd = NULL,
examinees = NULL,
instances_num = 1,
random = TRUE,
reorder_exercises = TRUE,
select_n_exercises = NULL) {
stopifnot("We need a template to define an exam." = !is.null(rmd))
if (!is.null(examinees)) {
examinees <- unique(examinees)
instances_num <- length(examinees)
} else {
examinees <- num_vector(end = instances_num)
}
instances <- num_vector(end = instances_num)
exercises <- data.frame(
type = character(),
statement = character(),
image = character(),
image_alt = character(),
answer = character(),
a_1 = character(),
a_2 = character(),
a_3 = character(),
stringsAsFactors = FALSE
)
structure(
list(
rmd = rmd,
a_n = 3,
exercises = exercises,
examinees = examinees,
instances = instances,
random = random,
reorder_exercises = reorder_exercises,
select_n_exercises = select_n_exercises,
delivery = TRUE,
seed = 173
),
class = "exam"
)
}
#' Generate the exam document
#'
#' From an exam object, we generate different instances of the exam to deliver to
#' the individuals being examined. To do this, we need to specify the folder where
#' they will be generated (using parameter `out_dir`), the output format (using
#' parameter `output_format`), the encoding (using parameter `encoding`), and whether
#' we want each exercise to start on a new page, include exercises until the pages
#' are filled, or preserve the definition of the exercise in this regard (using
#' parameter `new_pages` with the values 'all', 'none', or NULL).
#'
#' @param ex An `exam` object.
#' @param out_dir A string indicating the output folder.
#' @param output_format A vector of strings specifying the desired output formats.
#' @param encoding A string specifying the encoding.
#' @param new_pages A string with the values 'all', 'none', or NULL.
#'
#' @return An `exam` object.
#'
#' @family exam definition
#'
#' @examples
#'
#' \dontrun{
#' rmd <- system.file("extdata/template01.Rmd", package = "rexer")
#' exercises <- system.file("extdata/exercises.csv", package = "rexer")
#' ex <- exam(
#' rmd = rmd,
#' examinees = NULL,
#' instances_num = 1,
#' random = TRUE,
#' reorder_exercises = TRUE
#' ) |>
#' define_exercises_from_csv(exercises) |>
#' generate_document(out_dir = tempdir(), new_pages = 'all')
#' }
#'
#' @export
generate_document <-
function(ex,
out_dir,
output_format,
encoding,
new_pages)
UseMethod("generate_document")
#' @rdname generate_document
#' @export
generate_document.exam <- function(ex,
out_dir = NULL,
output_format = "pdf_document",
encoding = "UTF-8",
new_pages = NULL) {
if (!is.null(out_dir)) {
out_dir <- name_with_nexus(out_dir)
}
set.seed(ex$seed)
exam_number <- 1
n <- nrow(ex$exercises)
if (is.null(ex$select_n_exercises)) {
select_n_exercises <- n
} else if (ex$select_n_exercises > n) {
select_n_exercises <- n
} else {
select_n_exercises <- ex$select_n_exercises
}
sel_exercises <- ex$exercises
if (!is.null(new_pages)) {
if (tolower(new_pages) == 'all') {
sel_exercises$type <- "p"
} else {
sel_exercises$type <- ""
}
}
for (examinee in ex$examinees) {
if (select_n_exercises < n) {
i <- sample.int(n, select_n_exercises)
if (!ex$reorder_exercises) {
i <- sort(i)
}
sel_exercises <- ex$exercises[i, ]
}
exercises <-
interpret_exercises(sel_exercises,
exam_number,
ex$random,
ex$reorder_exercises,
ex$delivery)
all_exercises <-
interpret_all_exercises(sel_exercises,
exam_number,
ex$random,
ex$reorder_exercises,
ex$delivery)
rmarkdown::render(
ex$rmd,
output_format,
output_file = paste0(out_dir, snakecase::to_snake_case(examinee)),
encoding = encoding,
params = list(
exam_number = exam_number,
exam_number_str = ex$instances[exam_number],
examinee = examinee,
exercises = exercises,
all_exercises = all_exercises
)
)
exam_number <- exam_number + 1
}
ex
}
#' Generate the support document for exam correction
#'
#' From an exam object, we can generate instances that serve as support for the
#' correction of the exam. Each instance will include the answers, if they are
#' indicated, associated with the exercises. In any case, the randomly included
#' part of the exam will be highlighted.
#'
#' To do this, we specify the folder where the documents will be generated (using
#' parameter `out_dir`), the output format (using parameter `output_format`), the
#' encoding (using parameter `encoding`), and whether we want each exercise to
#' start on a new page, include exercises until the pages are filled, or preserve
#' the definition of the exercise in this regard (using parameter `new_pages`).
#'
#' @param ex An `exam` object.
#' @param out_dir A string indicating the output folder.
#' @param output_format A vector of strings specifying the desired output formats.
#' @param encoding A string specifying the encoding.
#' @param new_pages A string with the values 'all', 'none', or 'NULL'.
#'
#' @return An `exam` object.
#'
#' @family exam definition
#'
#' @examples
#'
#' \dontrun{
#' rmd <- system.file("extdata/template01.Rmd", package = "rexer")
#' exercises <- system.file("extdata/exercises.csv", package = "rexer")
#' ex <- exam(
#' rmd = rmd,
#' examinees = NULL,
#' instances_num = 1,
#' random = TRUE,
#' reorder_exercises = TRUE
#' ) |>
#' define_exercises_from_csv(exercises) |>
#' generate_correction_document(out_dir = tempdir())
#' }
#'
#' @export
generate_correction_document <-
function(ex,
out_dir,
output_format,
encoding,
new_pages)
UseMethod("generate_correction_document")
#' @rdname generate_correction_document
#' @export
generate_correction_document.exam <-
function(ex,
out_dir = NULL,
output_format = "pdf_document",
encoding = "UTF-8",
new_pages = NULL) {
ex_corr <- ex
ex_corr$delivery <- FALSE
ex_corr <-
generate_document(ex_corr, out_dir, output_format, encoding, new_pages)
ex
}
#' interpret all exercise
#'
#' @param exercises A data frame, exercises.
#' @param exam_number An integer, exam sequence number
#' @param random A boolean, is random generation.
#' @param reorder A boolean, reorder exercises.
#' @param delivery A boolean, is delivery version.
#'
#' @return A string.
#' @keywords internal
interpret_all_exercises <-
function(exercises,
exam_number,
random,
reorder,
delivery) {
if (reorder) {
nq <- nrow(exercises)
r <- sample(1:nq, nq, replace = FALSE)
exercises <- exercises[r, ]
}
txt <- ''
for (i in 1:nrow(exercises)) {
exercise <-
interpret_an_exercise(exercises[i,], exam_number, random, delivery)
if (exercises$type[i] == 'p' & i > 1) {
txt <- paste0(txt, '
\\newpage
')
}
txt <- paste0(txt, '
**', i, '.** ', exercise)
}
txt
}
#' interpret exercises
#'
#' @param exercises A data frame, exercises.
#' @param exam_number An integer, exam sequence number
#' @param random A boolean, is random generation.
#' @param reorder A boolean, reorder exercises.
#' @param delivery A boolean, is delivery version.
#'
#' @return A string vector.
#' @keywords internal
interpret_exercises <-
function(exercises,
exam_number,
random,
reorder,
delivery) {
nq <- nrow(exercises)
if (reorder) {
r <- sample(1:nq, nq, replace = FALSE)
} else {
r <- 1:nq
}
vq <- NULL
for (i in r) {
exercise <-
interpret_an_exercise(exercises[i, ], exam_number, random, delivery)
vq <- c(vq, exercise)
}
vq
}
#' interpret a exercise.
#'
#' @param exercise A data frame, exercise.
#' @param exam_number An integer, exam sequence number
#' @param random A boolean, is random generation.
#' @param delivery A boolean, is delivery version.
#'
#' @return A string.
#' @keywords internal
interpret_an_exercise <-
function(exercise, exam_number, random, delivery) {
names <- names(exercise)
base <- c("type", "statement", "image", "image_alt", "answer")
values <- setdiff(names, base)
others <- exercise[, values]
others <- others[, others != '']
txt <- exercise[, "statement"]
avoid <- integer(0)
if (exercise[, "image"] != '') {
txt <- paste0(txt,
'
![',
exercise[, "image_alt"],
'](',
exercise[, "image"],
')
')
r <-
unlist(stringr::str_extract_all(exercise[, "image"], "\\[\\[\\d\\]\\]"))
avoid <- as.integer(stringr::str_extract(r, "\\d"))
avoid <- avoid[!is.na(avoid)]
}
txt <- reorder_items(txt)
answer <- exercise[, "answer"]
answer <- string_to_vector(answer)
n_answers <- length(answer)
if (n_answers > 1) {
if (random) {
sel_answer <- sample.int(n_answers, 1)
answer <- answer[sel_answer]
} else {
answer <- select_sequential(answer, n = exam_number)
}
} else {
sel_answer <- 0
}
i <- 0
for (s in seq_along(others)) {
vector <- string_to_vector(others[[s]])
if (random) {
if (sel_answer > 0) {
sel <- select_sequential(vector, n = sel_answer)
} else {
sel <- select_random(vector, n = 1)
}
} else {
sel <- select_sequential(vector, n = exam_number)
}
i <- i + 1
pattern <- paste0('[[', i, ']]')
if (!delivery) {
if (!(i %in% avoid)) {
sel <- paste0('**[[', i, ': ', sel, ']]**')
}
}
txt <- gsub(pattern, sel, txt, fixed = TRUE)
}
if (!delivery) {
if (length(answer) > 0) {
txt <- paste0(txt,
'
**Answer:**
', answer, '
')
}
}
txt
}
#' reorder items.
#'
#' @param txt A string.
#'
#' @return A string.
#'
#' @keywords internal
reorder_items <- function(txt) {
x <- stringr::str_extract_all(txt, "\\{\\{([[:print:]]+)\\}\\}")
for (i in seq_along(x)) {
r <- unlist(stringr::str_extract_all(x[[i]], "\\[\\[\\d\\]\\]"))
if (length(r) > 0) {
v <- as.integer(stringr::str_extract(r, "\\d"))
v <- v[!is.na(v)]
l <- length(v)
s <- sample.int(l, l)
w <- v[s]
y <- x[[i]]
for (j in seq_along(v)) {
y <-
gsub(paste0('[[', v[j], ']]'),
paste0('[[XX', w[j], ']]'),
y,
fixed = TRUE)
}
y <- gsub(paste0('[[XX'), paste0('[['), y, fixed = TRUE)
y <- gsub(paste0('{{'), '', y, fixed = TRUE)
y <- gsub(paste0('}}'), '', y, fixed = TRUE)
txt <- gsub(x[[i]], y, txt, fixed = TRUE)
}
}
txt
}