I am using R markdown to create a PDF R course. I want to insert a quiz like the following:
---
output: pdf_document
---
What is the class of the following R object?
1. `pi`
```{r}
class(pi)
```
Which, as expected, creates a PDF with this content:
However, I would like the reader to not have such an easy access to the answer. These are the ideas I've had so far to achieve this:
>!
code of stackoverflow to hide spoilers (no idea if there is such a feature on R markdown, though);To me, the third idea seems like the most elegant way to do this, but I don't know how to implement it. I've taken a look at How to hide code in RMarkdown, with option to see it, http://yihui.name/knitr/options/ and https://www.ctan.org/pkg/exam?lang=en, but found nothing I could use.
As you can see, I don't mind if the solution requires the user to read the document on a computer, but if I can find a solution that would also work on printed versions of the document, that would be great.
include = FALSE prevents code and results from appearing in the finished file. R Markdown still runs the code in the chunk, and the results can be used by other chunks. echo = FALSE prevents code, but not the results from appearing in the finished file.
To transform your markdown file into an HTML, PDF, or Word document, click the “Knit” icon that appears above your file in the scripts editor. A drop down menu will let you select the type of output that you want. When you click the button, rmarkdown will duplicate your text in the new file format.
Chunk options You use results="hide" to hide the results/output (but here the code would still be displayed). You use include=FALSE to have the chunk evaluated, but neither the code nor its output displayed.
echo=TRUE shows the code, where echo=FALSE would hide the code. eval=TRUE means to evaluate (run) the code, where eval=FALSE will just show the code but not evalutate it.
I wasn't thinking of a full shiny app, but something like this .Rmd
---
output: html_document
---
## q1 what is `class(pi)?`
<div id="spoiler" style="display:none">
```{r}
class(pi)
```
</div>
<button title="Click to show answer" type="button"
onclick="if(document.getElementById('spoiler') .style.display=='none')
{document.getElementById('spoiler') .style.display=''}
else{document.getElementById('spoiler') .style.display='none'}">
Show/hide
</button>
And then to click
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With