library(exams)
# Initialize lists for the questions, answers, solutions, types, explanations (tolerances are not needed if there are no numeric questions)
<- list()
answers <- list()
solutions <- list()
types <- list()
explanations
# Define Question 1: What does the term "molar mass" refer to?
1]] <- c(
answers[["The number of molecules in one mole of a substance",
"The mass of one mole of a substance",
"The number of atoms in one mole of a substance",
"The charge of a mole of ions"
)1]] <- explanations[[1]] <- c(FALSE, TRUE, FALSE, FALSE) # The second option is correct
solutions[[1]] <- "schoice"
types[[
# Define Question 2: Which of the following is the lightest element?
2]] <- c("Oxygen", "Hydrogen", "Carbon", "Helium")
answers[[2]] <- explanations[[2]] <- c(FALSE, TRUE, FALSE, FALSE) # The second option (Hydrogen) is correct
solutions[[2]] <- "schoice" types[[
Single Choice Example
This is an exercise containing two single choice questions about molar massses.
The whole example as an .Rmd-file can be downloaded here:
The content of the file can be viewed by expanding the following field.
Note that the displayed code below is not highlighted as it would be in the downloaded file, so downloading it is recommended for better readability!
```{r answers/solutions, echo = FALSE, results = "hide"}
library(exams)
# Initialize lists for the questions, answers, solutions, types, explanations (tolerances are not needed if there are no numeric questions)
answers <- list()
solutions <- list()
types <- list()
explanations <- list()
# Define Question 1: What does the term "molar mass" refer to?
answers[[1]] <- c(
"The number of molecules in one mole of a substance",
"The mass of one mole of a substance",
"The number of atoms in one mole of a substance",
"The charge of a mole of ions"
)
solutions[[1]] <- explanations[[1]] <- c(FALSE, TRUE, FALSE, FALSE) # The second option is correct
types[[1]] <- "schoice"
# Define Question 2: Which of the following is the lightest element?
answers[[2]] <- c("Oxygen", "Hydrogen", "Carbon", "Helium")
solutions[[2]] <- explanations[[2]] <- c(FALSE, TRUE, FALSE, FALSE) # The second option (Hydrogen) is correct
types[[2]] <- "schoice"
```
Question========
*Question 1*
What does the term "molar mass" refer to?
##ANSWER1##
*Question 2*
Which of the following is the lightest element?
##ANSWER2##
```{r questionlist, echo = FALSE, results = "asis"}
answerlist(unlist(answers), markup = "markdown")
```
Solution========
*Solution 1*
The molar mass is the mass of one mole of a substance, expressed in grams per mole (g/mol).
*Solution 2*
Hydrogen is the lightest element, with an atomic mass of approximately 1 g/mol.
```{r solutionlist, echo = FALSE, results = "asis"}
# Convert solutions to "True" or "False"
explanations <- lapply(solutions, function(x) ifelse(x, "True", "False"))
solutions <- lapply(solutions, mchoice2string)
# Display explanations
answerlist(unlist(explanations), markup = "markdown")
```
Meta-information================
exname: example_schoice
extitle: molmass and lightest element
extype: cloze`r paste(solutions, collapse = "|")`
exsolution: `r paste(types, collapse = "|")`
exclozetype:
exshuffle: TRUE exversion: v1
After conversion with R/exams
the exercise looks like this for different output types.
Creating the List Structure
This section creates and fills the lists which contain the questions, answers, etc.. The answers
list contains the answer options for the questions. The solution to the question, the type of question, as well as a more elaborate explanation are specified for each question.
The following code has to be placed within a code chunk in the .Rmd-file. See the first problem in this section of the FAQ
Question Section
The next section contains the question text, including the marked spaces ##ANSWERi##
for the answer fields and a necessary code section, which does some formating which is required for the later conversion. This is markdown text, which will appear bold/italic/etc. after conversion.
It also includes a code chunk, which converts the answers
list to the necessary format.
Question========
*Question 1*
What does the term "molar mass" refer to?
##ANSWER1##
*Question 2*
Which of the following is the lightest element?
##ANSWER2##
```{r questionlist, echo = FALSE, results = "asis"}
answerlist(unlist(answers), markup = "markdown")
```
Solution Section
In this section, additional information and/or the exact solutions can be given. In this case, there is a more elaborate explanation as a solution for each question. Additionally, the code chunk included below converts the TRUE
and FALSE
objects into a more readable format of “True” and “False”, however this could be in any other format as well.
Solution========
*Solution 1*
The molar mass is the mass of one mole of a substance, expressed in grams per mole (g/mol).
*Solution 2*
Hydrogen is the lightest element, with an atomic mass of approximately 1 g/mol.
```{r solutionlist, echo = FALSE, results = "asis"}
# Convert solutions to "True" or "False"
explanations <- lapply(solutions, function(x) ifelse(x, "True", "False"))
solutions <- lapply(solutions, mchoice2string)
# Display explanations
answerlist(unlist(explanations), markup = "markdown")
```
Meta-Information
The next section contains the meta information needed for conversion. See here for an explanation of the meta-information.
The “source code” is written in markdown and contains inline R code.
Meta-information================
exname: example_schoice
extitle: molmass and lightest element
extype: cloze`r paste(solutions, collapse = "|")`
exsolution: `r paste(types, collapse = "|")`
exclozetype:
exshuffle: TRUE exversion: v1
If the “code” above is evaluated, the section looks like this:
Meta-information================
exname: example_schoice
extitle: molmass and lightest element
extype: cloze
exsolution: c(FALSE, TRUE, FALSE, FALSE)|c(FALSE, TRUE, FALSE, FALSE)
exclozetype: schoice|schoice
exshuffle: TRUE exversion: v1