-
Notifications
You must be signed in to change notification settings - Fork 2
/
custom_questions.Rmd
104 lines (84 loc) · 4.02 KB
/
custom_questions.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
---
title: "Customizing Questions"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Customizing Questions}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
eval = FALSE
)
```
```{r warning=FALSE, message=FALSE, include=FALSE}
pkgload::load_all()
```
`shinyquiz` provides versatile functions customizing your questions beyond the predefined question types. By using `create_question()` and `create_question_raw()` functions, you can tailor your quizzes to suit a wide array of educational scenarios and learning styles.
In this guide, we will delve into the power of these two functions, demonstrating how to craft custom questions and incorporate raw HTML for enhanced question formatting.
```{r setup}
library(shinyquiz)
```
## Maximizing `create_question()`
`create_question()` automatically generates the necessary HTML to create the question prompt. However, it can also take display custom HTML. `htmltools` and `shiny` include many functions to convert R code into HTML.
For example, a custom plot can be included in the question by passing a plot to `shiny::renderPlot()` and wrapping the entire prompt using `htmltools::tagList()`. Note that some HTML functions will not print correctly so using `shinyquiz::preview_app()` is recommended.
```{r eval=FALSE}
create_question(
htmltools::tagList(
htmltools::p("A custom question with a plot"),
shiny::renderPlot(plot(rnorm(100), rnorm(100))),
),
add_slider(10, 50, 30, correct = 20),
label = 'Select 20'
)
```
<br>
<p align="center">
<img src="figures/custom-questions-plot.png" style="width: 90%; max-width: 500px;">
</p>
<br>
```{r eval=FALSE}
create_question(
htmltools::tagList(
htmltools::p("A custom question with a table"),
reactable::reactable(mtcars),
),
add_slider(10, 50, 30, correct = 20),
label = 'Select 20'
)
```
<br>
<p align="center">
<img src="figures/custom-questions-table.png" style="width: 90%; max-width: 500px;">
</p>
<br>
## Full Customization with `create_question_raw()`
`create_question_raw()` gives you more control over the question and answer options. You can use any Shiny input function to create the choices, and you define your own grading logic.
Three elements are required for `create_question_raw()`:
- `prompt`: An HTML div defining the question and any accompanying elements. You can use HTML tags, Shiny inputs, or any other UI elements. This is where you set up what the user will see. At a minimum this should include a shiny `input` object that will be used to collect the user's answer.
- `grader`: A function to takes the user input as an argument and returns back TRUE or FALSE. This gives you full control over the grading logic.
- `correct_answer_pretty`: Text representing the correct answer. Useful for displaying a "pretty" version in the quiz's summary table.
- `user_answer_prettifier`: An optional function that takes the user input as a argument and returns back a "pretty" version of it. Useful for displaying cleaner answers in the quiz's summary table. The default is `paste0(user_input, collapse = ", ")`.
For example, you could use `shiny::dateInput()` to specify a date input. Note the use of `shiny::NS('quiz')('answers')` to define the `inputId`. This is required for `shinyquiz` to find the user's answer.
```{r eval=FALSE}
create_question_raw(
prompt = htmltools::div(
htmltools::p("On what date was the U.S. Declaration of Independence signed?"),
shiny::dateInput(
inputId = shiny::NS('quiz')('answers'),
label = 'Select 5',
value = as.Date('1800-01-01')
)
),
grader = \(user_input) setequal(user_input, as.Date('1776-07-04')),
correct_answer_pretty = '5'
)
```
<br>
<p align="center">
<img src="figures/custom-questions-raw.png" style="width: 90%; max-width: 500px;">
</p>
<br>
Take caution when defining the `grader` function as some elements may return back an object of a different class that you are expecting. It's recommended to always self-test your quiz.