R-exams: How to change labels for multiple choice alternatives to distinguish from cloze items (a, b, ...) - r-exams

I am trying to create pdf exam sets using exams2pdf(...) using templates "exam.tex", "solution.tex". I want to have some questions of type "cloze", with e.g. both single choice questions and numeric questions. The problem is that the cloze question is itemized with (a), (b), ... The same labels (a), (b), ... are also used for the different possible answers to single choice questions. So, using the template questions "lm.Rmd" and "switzerland.Rmd" I get this in the question sheet:
While the front page answer sheet looks like this.
This obviously doesn't look great. I have tried to modify the "exam.tex" template by changing the "answerlist" environment definition, specifically changing
...\renewcommand{\labelenumi}{(\alph{enumi})}...
to e.g.
...\renewcommand{\labelenumi}{(\roman{enumi})}...
However, this appeared to have no effect on the output. I moreover tried to modify the \exmchoice definition, changing "(a)" to e.g. "(A)". This only affected the labels on the front page, not in the question sheet. The exams2pdf call in R looks as
exams2pdf(c("lm.Rmd", "switzerland.Rmd"), n = 1, name = c("MAT210-exam", "MAT210-solution"),
encoding = "UTF-8", dir = "output", edir = "../MAT210exercises",
template = c("../templates/exam.tex", "../templates/solution.tex"))
So, I hope someone can guide a solution route to enable different labels for cloze items and choice alternatives in this setting.
I am running R version 3.6.1 (2019-07-05). Windows 7, MikTex 2.9.

The behavior of the cloze formatting can be controlled "a little bit" by the cloze.collapse control argument. And I think you are looking for the "enumerate" option:
exams2pdf(c("lm.Rmd", "switzerland.Rmd"), template = "exam.tex",
control = list(cloze.collapse = "enumerate"))
This yields the following format in the questionnaire on the title page:
And the following format in the exercise itself:
Note that if the only target is PDF output, it might be easier to split up the combined cloze question into separate exercises. You can still share parameters/data across the exercises if you specify a dedicated environment envir= in which the exercises are processed (rather than using a fresh environment for every question). The drawback is that such sequencing of exercises is only available when producing standalone PDF files but cannot be used in learning management systems like Moodle because these all exercises separately. For further discussion of these issues, see the R/exams forum on R-Forge:
https://R-Forge.R-project.org/forum/forum.php?thread_id=33878&forum_id=4377&group_id=1337
https://R-Forge.R-project.org/forum/forum.php?thread_id=33912&forum_id=4377&group_id=1337

Related

R/exams: how to select different font type for pdfs

HI am pretty new to coding with R/LaTeX and currently trying to figure out how to set up exams using R/exams. While I have already managed to create the exercises themselves, I now want to save them as pdfs using exams2pdf.
However, I would like to use a different font than the one used by default. Does anyone know whether this is possible and, if so, how I have to add this to my code?
Is it also possible to change the "Header", where it says "1. Problem" to something else/remove it?
Thanks a lot for your help.
Overview
In exams2pdf() there is the template = argument with which you can specify a master LaTeX template into which the individual exercises are embedded. By providing your own template you can change any aspect you like: fonts, page formatting, intro text, whether or not questions/solutions are shown, etc.
Below I give a rather detailed overview so that this post is also useful for other R/exams users that want to modify other aspects of the template in exams2pdf().
Getting started
To get started I would recommend to go to a suitable working directory and then set up exams_skeleton() there, e.g.,
exams_skeleton(dir = ".", writer = "exams2pdf", markup = "markdown")
which copies all of the .Rmd exercises (due to the markup = "markdown" specification) available in R/exams to an exercises/ folder, along with all available LaTeX templates to the templates/ folder, and sets up two demo .R scripts: demo-all.R and demo-pdf.R. You can look at these, especially the latter, and play around with the examples provided there.
Vignette
More details and background information are then provided in a package vignette: vignette("exams", package = "exams"), especially Section 3.
The master LaTeX template
For doing the concrete adaptations you ask for, I would start by modifying the plain.tex template in the templates/ directory. The "factory-fresh" default content is:
\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{a4wide,color,Sweave,url,amsmath,booktabs,longtable,eurosym,amssymb}
\newenvironment{question}{\item \textbf{Problem}\newline}{}
\newenvironment{solution}{\textbf{Solution}\newline}{}
\newenvironment{answerlist}{\renewcommand{\labelenumii}{(\alph{enumii})}\begin{enumerate}}{\end{enumerate}}
\providecommand{\tightlist}{\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setkeys{Gin}{keepaspectratio}
\begin{document}
\begin{enumerate}
%% \exinput{exercises}
\end{enumerate}
\end{document}
Changing question and solution formatting
For example, you could decide to only show the question but not the solution by modifying the {question} and {solution} environments above to:
\usepackage{verbatim}
\newenvironment{question}{\item}{}
\newenvironment{solution}{\comment}{\endcomment}
Changing the answer list formatting
Then you could modify the answerlist from (a), (b), ... to A., B., by modifying the {answerlist} environment:
\newenvironment{answerlist}{\renewcommand{\labelenumii}{\Alph{enumii}.}\begin{enumerate}}{\end{enumerate}}
Changing the font
And finally you could modify the default font to Lato, say, by loading the {lato} package and using a dark gray (rather than the default black) font color. To do so insert before the \begin{document} line:
\usepackage[default]{lato}
\definecolor{darkgray}{rgb}{0.23,0.23,0.23}
\color{darkgray}
Run exams2pdf() with new template
And then to see the result, run:
exams2pdf("swisscapital.Rmd", edir = "exercises/", template = "templates/plain.tex")

Where is the documentation for the layout of the template .tex file for r/exams

I'm trying to generate a set of exams using R/exams (software of which I'm very impressed by the way). For our University College, we need to apply a certain template for our exams. We have a .sty file for it, but I'm unable to include it in the LaTex template. I have tried all the options per documentation
Using the inputs variable:
exams2pdf(myexam, n = 1, name = c("pdf-examen", "pdf-oplossing"),
encoding = "UTF-8",
showpoints = "TRUE",
dir = "output",
edir = "oefeningen",
inputs = "/Users/eothein/education/examentest/templates/hogent-examen.sty",
template = c("templates/opgave.tex", "templates/oplossing.tex"),
)
I have tried relative paths, absolute paths but the LaTeX compiler complains it cannot find the .sty file.
On top of that, it is very difficult to find documentation on how to use the metainfo/regular info from R into the LateX file. I'm starting from the template file generated from exams_skeleton but it is unclear how to work with it (without a lot of trial and error). Is there documentation for this? E.g. I guess %% \exinput{questionnaire} generates the questions, but how can I tweak this?
Kind regards
Jens Buysse
In general I would recommend to include such .sty files in your personal texmf tree, e.g., in texmf/tex/latex/hogent-examen.sty or texmf/tex/latex/hogent/hogent-examen.sty. This way they are found from every location on your machine.
Your solution via the inputs= argument should have worked as well, tough. It's hard to tell what exactly went wrong without a fully reproducible example.
As for your general question about the documentation for the exams2pdf() templates, see vignette("exams", package = "exams"), specifically Section 3.
Historical note: This is actually quite an old document (including various updates, though), originally written more than a decade ago and long before R/exams had the capabilities it has today. I should turn the most important details from that paper into a blog post for R-exams.org (which is less than 3 years old). However, I didn't get round to doing this, though.

How to create exam sheets with solutions in a non-English language with R exams

In order to create the exam sheets without solution, everything works fine with exams2nops (apart from the logo which is not taken for some reason). I can add the name of the institution, change the title, and setup a non-English language and the date. But if I want to create examsheets with the solutions, then, if I understand correctly, I cannot use anymore exams2nops, but I have to use exams2pdf with template="solution" which does not take into account the institution, the date, the language, nor the title.
Any help?
You are correct that exams2nops() does not provide a version of the PDF with the {solution} environment and the complete answer text. The nops_eval() only provides a HTML report with the correct boxes that needed to be checked.
So to produce a PDF as desired you can run exams2pdf(...) or exams2pdf(..., type = "solution") after setting the same random seed that you set before running exams2nops(). If you want to modify anything like layout/logo/language/etc. in exams2pdf() you have full flexibility but need to do the work yourself. That is you need to come up with a LaTeX template file that looks the way you want. As a starting point you can take solution.tex from the package. The easiest way to get all templates shipped with the package is to run exams_skeleton() and then look at the templates folder.
Background info: exams2nops() also just calls exams2pdf() internally after setting up a suitable LaTeX template file based on the arguments specified by the user.

Linking/importing externalized source code in org-mode

This paper inspired me to check out Emac's org-mode a bit and currently I try to assess what's more suitable for writing my documents: knitr/Sweave (I'm mainly using R to do my programming) or org-mode.
What I really like about knitr is the option to externalize the actual source (watch out: the declaration of labels/names in the R script seems to have changed from ## ---- label ------- to ## #knitr label; see ?read_chunk) and "link" them to the actual literate programming/reproducible research document (as opposed to actually writing the code in that very document):
"Import" with
<<import-external, cache=FALSE>>=
read_chunk('foo-bar.R') # has label/name 'foo-bar'
#
and "re-use" by referencing the respective labels with
<<foo-bar>>=
#
Question
Is this also possible in org-mode or am I bound to putting the actual code into the .org document?
I found this, but I did not find any specific notion of linking/importing external source code files and be able to execute them by having the linked code inside
#+BEGIN_SRC R
<linked code>
#+END_SRC
Background
I do see that this approach might contrast the general paradigm of literate programing to some extend. But I like to work in a somewhat "atomic" style and thus it feels more natural to me to keep the files separated at first and then mash everything together dynamically.
Would named code blocks help?
#+NAME: foo-bar
#+BEGIN_SRC R
source(foo-bar.R)
#+END_SRC
And then evaluate (i.e. load) the code when you actually need it:
#+CALL: foo-bar()
See manual for more details.

Using org-mode to structure an analysis

I am trying to make better use of org-mode for my projects. I think literate programming is especially applicable to the realm of data analysis and org-mode lets us do some pretty awesome literate programming.
I think most of you will agree with me that the workflow for writing an analysis is different than most other types of programming. I don't just write a program, I explore the data. And, while many of these explorations are dead-ends, I don't want to delete/ignore them completely. I just don't want to re-run them every time I execute the org file. I also tend to find or develop chunks of useful code that I would like to put into an analytic template, but some of these chunks won't be relevant for every project and I'd like to know how to make org-mode ignore these chunks when I am executing the entire buffer. Here's a simplified example.
* Import
- I want org-mode to ignore import-sql.
#+srcname: import-data
#+begin_src R :exports none :noweb yes
<<import-csv>>
#+end_src
#+srcname: import-csv
#+begin_src R :exports none
data <- read.csv("foo-clean.csv")
#+end_src
#+srcname: import-sql
#+begin_src R :exports none
library(RSQLite)
blah blah blah
#+end_src
* Clean
- This is run on foo.csv, producing foo-clean.csv
- Fixes the mess of -9 and -13 to NA for my sanity.
- This only needs to be run once, and after that, reference.
- How can I tell org-mode to skip this?
#+srcname: clean-csv
#+begin_src sh :exports none
sed .....
#+end_src
* Explore
** Explore by a factor (1)
- Dead end. Did not pan out. Ignore.
- Produces a couple of charts showing there is not interaction.
#+srcname: explore-by-a-factor-1
#+begin_src R :exports none :noweb yes
#+end_src
** Explore by a factor (2)
- A useful exploration that I will reference later in a report.
- Produces a couple of charts showing the interaction of my variables.
#+srcname: explore-by-a-factor-2
#+begin_src R :exports none :noweb yes
#+end_src
I would like to be able to use org-babel-execute-buffer and have org-mode somehow know to skip over the code blocks import-sql, clean-csv and explore-by-a-factor-1. I want them in the org file, because they are relevant to the project. After-all, tomorrow someone might want to know why I was so sure explore-by-a-factor-1 was not useful. I want to keep that code around, so I can bang out the plot or the analysis or what-ever and go on, but not have it run every-time I rerun everything because there's no reason to run it. Ditto with the clean-csv stuff. I want it around, to document what I did to the data (and why), but I don't want to re-run it every time. I'll just import foo-clean.csv.
I Googled all over this and read a bunch of org-mode mailing list archives and I was able to find a couple of ideas, but not what I want. EXPORT_SELECT_TAGS, EXPORT_EXCLUDE_TAGS are great, when exporting the file. And the :tangle header works well, when creating the actual source files. I don't want to do either of these. I just want to execute the buffer. I would like to be able to define code blocks in a similar fashion to be executed or ignored. I guess I would like to find a way to have an org variable such as:
EXECUTE_SELECT_TAGS
This way I could simply tag my various code blocks and be done with it. It would be even nicer if I could then run the file, using only source blocks with specific tags. I can't find a way to do this and I thought I would ask before asking/begging for a new feature in org-mode.
I figured out. From the Org manual (since updated):
The :eval header argument can be used to limit the evaluation of specific code blocks. :eval accepts two arguments “never” and “query”. :eval never will ensure that a code block is never evaluated, this can be useful for protecting against the evaluation of dangerous code blocks. :eval query will require a query for every execution of a code block regardless of the value of the org-confirm-babel-evaluate variable.
So you just have to add :eval never to the header of the blocks that you don’t want to execute, and voilá!
While I never did get an answer to my question, the discussion was interesting and apparently an org-mode based Template for R strikes a few people as an interesting idea. I downloaded the source code to org-mode and looked at org-babel-execute-buffer. It is, as I feared, a naive function which does precisely what it says it does and nothing more. It is not (currently) possible to pass it any additional parameters to affect it's behavior. (Unless I am badly misreading the lisp, which is entirely possible.)
Eventually, I decided org-babel-execute-buffer is not necessary for a useful R template system. Babel's noweb functionality is really flexible and I think it is possible to build a workable solution using noweb, rather than trying to develop a complex tagging schema to define how/when to run things.
For tangling/export it should still be possible to use tags to create usable/sane output.
For anyone who is interested: LiterateR
It's probably a little rude to use this thread to put this out there but this is why I asked the question in the first place. TemplateR is my attempt to make R a little easier to use. Right now it is just a template with two simplistic functions. I consider it to be a proof of concept at this point. Eventually, I want to develop something that does more to help people develop R projects more quickly. TemplateR will accomplish this by:
1. Provide a strong structure to develop around.
2. Provide built-in function to provide support for common tasks, especially in the realm of reproducible research.
3. Provide snippets of tested code that can be rapidly re-purposed for the current project.
Right now, all it provides is a basic structure/framework and two simple functions.
1. Identify which R packages are missing (based on what is manually entered into a table) and
2. Creates project directories (plots, data, reports).
More will come in future versions. The README.org and TODO.org go into further detail.

Resources