Search code examples
rggplot2r-markdownr-exams

Is there support for ggplot2 in the exams package?


I have been trying to create an exam question in r markdown for the exams package that includes a ggplot2 plot.

Here is a reprex:

```{r}
library(ggplot2)
library(tibble)
x <- data_frame(x = rnorm(100, 50, 25), y = x + rnorm(100, 0, 15))
```

Question
========
```{r}
ggplot(x) + geom_point(aes(x = x, y = y))
```

What is true about the plot?

Answerlist
----------
* The relationship is strong
* The relationship is weak

Solution
========
The relationship is strong.

Answerlist
----------
* True
* False

Meta-information
================
exname: scatterplot
extype: schoice
exsolution: 10
exshuffle: 2

If I knit the document, the html page previews the plot as it should.

enter image description here

However if I use the exams2html() function, the ggplot is not shown.

enter image description here

I've tried different ways to print the plot (like storing the plot in a variable, then print, or used inline code, etc.) but nothing really worked. I could not find any solution on the r-exams.org site, or anywhere else. So, my question is: am I missing something or there is no ggplot2 support in the exams package yet?


Solution

  • This is not related to ggplot2. It is about some defaults in knitr plus an oversight on my side.

    tl;dr: The problem is avoided now in the devel version of exams on R-Forge (https://R-Forge.R-project.org/R/?group_id=1337). After installing this version the example should work ok: install.packages("exams", repos="http://R-Forge.R-project.org")

    Details: The code chunk

    ```{r}
    ggplot(x) + geom_point(aes(x = x, y = y))
    ```
    

    is processed correctly into a grahics file (as you already showed in your question). However, the resulting graphics file is stored in a sub-directory figure/ which is not searched by the exams2xyz() functions for potential supplements. And because the actual HTML file is set up in a different directory than all the temporary .Rmd and .md files, the figure is not found.

    The simple solution is to use the current directory for all supplements instead of sub-directories. Additionally, I would switch off the caption:

    ```{r, fig.path = "", fig.cap = ""}
    ggplot(x) + geom_point(aes(x = x, y = y))
    ```
    

    With this modification exams2html() and exams2pdf() both work fine. Note that the knitted files (.md and graphics) are generated in a temporary folder and the HTML is generated in another temporary folder so that none of this clutters your work space.

    In case you want to force the inclusion of the graphic without floating and without a caption in the PDF output you need to include a \ line before the code chunk. See http://www.R-exams.org/templates/scatterplot/ in the R/exams template gallery for a worked example (using base graphics but otherwise very similar).

    My oversight: I had set fig.path="" via knitr::opts_chunk$set() inside the exams package for .Rnw files but had forgotten to do the same for .Rmd files. This has been remedied in the devel version. Thus, you can either resort to that or explicitly include fig.path="" in your exercise which also works with the CRAN release version.