Generate bordered tables with exams::exams2mooodle
July 24, 2020
R
exams
Motivation
By default, tables in moodle problems created with exams::exams2moodle
do not have table or cell borders. I want tables with borders!
If you does too, you might find this post useful.
Stylesheet
Create a style for a class you will use for tables. Note that this is not a CSS file but a HTML code snippet that you want to include in markdown files.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<style type="text/css" rel="stylesheet"> | |
.bordered-table { | |
border-collapse: collapse; | |
border-spacing: 0; | |
border:1px solid #000000; | |
} | |
.bordered-table th { | |
border:1px solid #000000; | |
} | |
.bordered-table td{ | |
border:1px solid #000000; | |
} | |
</style> |
Instead of styling a class, it’s possible to style table
tag itself. But I strongly discourage you from doing this. That will affect the appearance of other tables used in Moodle.
Make a problem with tables
Two important points:
- Embed the style under the Question section. I would use something like
writeLines(readLines("https://git.io/JJ4uR")
. - When you make a table, use HTML format and add the class attribute to the table tag (
bordered-table
).knitr::kable()
hastable.attr
argument that does this for you.
Example
Here’s a sample problem.
```{r}
N <- 3
mountains <-
data.frame(
Mountain = c("Everest", "K2", "Kangchenjunga", "Lhotse", "Makalu",
"Cho Oyu", "Dhaulagiri", "Manaslu"),
`Height (m)` = c(8848, 8611, 8586, 8516, 8485, 8188, 8167, 8163),
check.names = FALSE
)
choice <- sample(nrow(mountains), N)
table <- mountains[choice, ]
table$`Height (m)` <- paste0("\\##ANSWER", seq_len(N), "##")
```
Question
========
```{r CSS, echo = FALSE, results = "asis"}
writeLines(readLines("https://git.io/JJ4uR"))
```
How high are the following mountains? Answer the height above sea level in meters.
```{r question, echo = FALSE, results = "asis"}
knitr::kable(table, format = "html",
table.attr='class="bordered-table"',
align = "lr",
row.names = FALSE)
```
Solution
========
```{r solution, echo = FALSE, results = "asis"}
knitr::kable(mountains[choice, ], format = "html",
table.attr = 'class="bordered-table"',
row.names = FALSE)
```
Meta-information
================
extype: cloze
exsolution: `r paste(mountains[choice, "Height (m)"], collapse = "|")`
exclozetype: `r paste(rep("num", 3), collapse = "|")`
exname: bordered-table
extol: 0
exextra[numwidth,logical]: TRUE
Result
You will get a quiz and solution that look like the following screenshots.