Numeric Example

This is an example about molar masses of hydrogen, oxygen and H20, which contains two numeric questions.

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 data generation, echo = FALSE, results = "hide"}
library(exams)
#atomic number of H
n_H <- 1
# Define the molar masses of hydrogen (H) and oxygen (O)
m_h <- 1.008 # molar mass of hydrogen in g/mol
m_o <- 16.00 # molar mass of oxygen in g/mol

# Calculate the molar mass of the water molecule (H2O)
m_h2o <- 2 * m_h + m_o
```

```{r answers/solutions, echo = FALSE, results = "hide"}
# Create lists for necessary information
answers <- list()
solutions <- list()
types <- list()
explanations <- list()
tolerances <- list()

# Define Question 1: atomic number of hydrogen
answers[[1]] <- ""  # No predefined answer for numeric
solutions[[1]] <- n_H
types[[1]] <- "num" #numeric
explanations[[1]] <- "The atomic number of hydrogen is $1$ because it has one proton in its nucleus."
tolerances[[1]] <- 0  # No tolerance

# Define Question 2: molar mass of water
answers[[2]] <- "" # no predefined answer for numeric
solutions[[2]] <- m_h2o
types[[2]] <- "num" 
explanations[[2]] <- "The molar mass of H2O is calculated by adding the molar masses of two hydrogen atoms and one oxygen atom."
tolerances[[2]] <- 0.5 # Allow for small rounding errors
```

Question
========

*Question 1*

What is the atomic number of hydrogen in the periodic table? ##ANSWER1##

*Question 2*

Calculate the molar mass of water ($H_2O$). Use the following molar masses:

-   Hydrogen ($H$): `r m_h` $g/mol$

-   Oxygen ($O$): `r m_o` $g/mol$

Provide your answer in grams per mole ($g/mol$). ##ANSWER2##

```{r questionlist, echo = FALSE, results = "asis"}
answerlist(unlist(answers), markup = "markdown")
```

Solution
========

```{r solutionlist, echo = FALSE, results = "asis"}
answerlist(unlist(explanations), markup = "markdown")
```

$M_{H_{2}O} = 2 \cdot M_H + M_O = 2 \cdot `r m_h` + `r m_o` = `r m_h2o`$ (g/mol)

Meta-information
================
exname: example_num 
extitle: molmass of water 
extype: cloze 
exsolution: `r paste(solutions, collapse = "|")` 
exclozetype: `r paste(types, collapse = "|")` 
extol: `r paste(tolerances, collapse = "|")` 
exshuffle: TRUE 
exversion: v1

After conversion with R/exams the exercise looks like this for different output types.

Data Generation

First is the so-called data generating process. In this case no real data is being generated within in this code chunk, it just includes some preparatory work of defining variables and creating the objects needed later.

For a simple example, giving this process its own section might seem a bit like going overboard, however, once examples become more complex, it is easier to handle this as the first part in a separate section.

Warning

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

library(exams) #exams package needs to be loaded for all functions to be available 

#atomic number of H
n_H <- 1
# Define the molar masses of hydrogen (H) and oxygen (O)
m_h <- 1.008 # molar mass of hydrogen in g/mol
m_o <- 16.00 # molar mass of oxygen in g/mol

# Calculate the molar mass of the water molecule (H2O)
m_h2o <- 2 * m_h + m_o

Creating the List Structure

The next step is to create and fill the lists needed for creating the exam. This is done within a code chunk. (See the structure of an .Rmd-file here, the creation here and the conversion process here)

First the empty lists are created and then they are filled question by question. Each question can have answer options, however for numeric questions, these are not needed. (See here and here for single and multiple choice examples). A solution is provided for each question using the predefined variables from the data generation section. Using named variables instead of the number itself, gives a better overview and allows for easier adjustment, as only the number in the data generating section has to be adjusted. The type also has to be specified for each question, in this case “num” for numeric. In addition, explanations are also added, these are optional but provide extra information as feedback. The tolerances are set in a separate list as well.

Warning

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.

# Create lists for necessary information
answers <- list()
solutions <- list()
types <- list()
explanations <- list()
tolerances <- list()

# Define Question 1: atomic number of hydrogen
answers[[1]] <- ""  # No answer options needed for numeric questions
solutions[[1]] <- n_H
types[[1]] <- "num" #numeric
explanations[[1]] <- "The atomic number of hydrogen is $1$ because it has one proton in its nucleus."
tolerances[[1]] <- 0  # No tolerance

# Define Question 2: molar mass of water
answers[[2]] <- "" # no answer options needed for numeric questions. 
solutions[[2]] <- m_h2o
types[[2]] <- "num" 
explanations[[2]] <- "The molar mass of H2O is calculated by adding the molar masses of two hydrogen atoms and one oxygen atom."
tolerances[[2]] <- 0.5 # Allow for small rounding errors

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.

Question
========

*Question 1*

What is the atomic number of hydrogen in the periodic table? ##ANSWER1##

*Question 2*

Calculate the molar mass of water ($H_2O$). Use the following molar masses:

-   Hydrogen ($H$): `r m_h` $g/mol$

-   Oxygen ($O$): `r m_o` $g/mol$

Provide your answer in grams per mole ($g/mol$). ##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, the explanations from the explanations list are pasted here and an additional formula is provided.

Solution
========

```{r solutionlist, echo = FALSE, results = "asis"}
answerlist(unlist(explanations), markup = "markdown")
```

$M_{H_{2}O} = 2 \cdot M_H + M_O = 2 \cdot `r m_h` + `r m_o` = `r m_h2o`$ (g/mol)

Meta Information Section

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_num 
extitle: molmass of water 
extype: cloze 
exsolution: `r paste(solutions, collapse = "|")` 
exclozetype: `r paste(types, collapse = "|")` 
extol: `r paste(tolerances, collapse = "|")` 
exshuffle: TRUE 
exversion: v1

If the “code” above is evaluated, the section looks like this:

Meta-information
================
exname: example_num
extitle: molmass of water 
extype: cloze
exsolution: 1|18.016
exclozetype: num|num
extol: 0|0.5
exshuffle: TRUE
exversion: v1