R exams template

PDF exams in R with exams

Last month I described a way of creating PDF exam papers that integrate R code. This method used LaTeX to typeset a document with the document class {exam}. This way you could leverage the beautiful advantages of LaTeX within the R markdown environment.

There is an another way of creating PDF exam papers with R code in R markdown I didn’t mention! The R package exams has a function exams2pdf that will create a PDF exam sheet (and solutions!) for you using the exams workflow. I’ve written about exams before. Check it out if you need an introduction to the package. The package is pretty handy but quite technical. I use the package to create randomised MCQ tests for Blackboard.

The advantages of the exams workflow is that the content is dynamic and it’s easy to randomise the questions and their content. The questions I use to create a Blackboard test can be used to create a paper test. The disadvantage is that the questions are limited to the types that exams supports, which is great for MCQ but not for short answer or essays. For short answer exams, you’re better off using LaTeX.

The default settings/examples of exams2pdf are not that helpful to deploy an exam right away. There is a TeX template with basic features but that is for demonstration only. Here’s what you need to change to generate a more usable exam sheet with a cover page, an answer page, and customized headers. I’m assuming that you have a bank of questions ready to go.


Modifying the TeX template

You can save a copy of the template from exams in your working directory and start from that. The question and the solution sheet have different templates called exam and solution, respectively.
Alternatively, my resulting TeX templates (and script) are on Gist.

Aside from minor aesthetics like changing text, I’ve made two major modifications to the default template:

  1. Allowing a user-defined class name as the title of the exam (called Class). In LaTeX this is the variable \myClass
  2. Placed the answer sheet on a new page. The original had it on the cover sheet but if you have a lot of questions then it spans two pages.

The R script

The R script to generate the exam is simple: get a list of questions then run exams2pdf with the right variables. Here is the script (also on Gist):

# Creates PDF exam 
# Write questions as Rmd, use latex symbols instead of HTML

# get files as list from directory. Could pool questions as well.
questions <- as.list(list.files("<directory>",
                                pattern = "*.Rmd",
                                full.names = TRUE))

exams::exams2pdf(questions,
                 # list of questions
                 n = 10,
                 # Number of unique copies
                 template = c("PDF template/exam", "PDF template/solution"),
                 # Use template
                 name = c("Blank", "Answer"),
                 # Give names to files
                 dir = "<output directory>",
                 # Location to save files
                 header = list(Date = "2023-02-01",
                               ID = "Code2023",
                               Class = "Stats")
                 # Exam information
                 )

Most variables are the same if you’ve used exams before. The key differences are:

  • template: the address of your modified TeX files. If blank, then the default package one is used which shows the answers and solutions - not very useful. Using c("exam", "solution") will use the default package ones - also not very useful.
  • name: This is a vector of file names. You need two names because this script is going to generate two versions of the exam: the blank question sheet and the solution.
  • header: These are variables passed to LaTeX to fill in the corresponding fields on the document. Date, class ID and class name can be changed.

Now you are ready to print the files!

Avatar
Jacinta Kong
Postdoctoral Fellow

My research interests include species distributions, phenology & climate adaptation of ectotherms.

Previous

Related