I am doing some exercises for my students using RMarkdown and a piece of code I was using begore, from Ronak Bhatt, but it is not working corretly anymore. Instead R code in correct format, I am having ```r ... like a text output.
Below I am posting all setting to have a button that hide/unhide the code in html. I really appreciate any help. Thanks!
In the R Markdown yaml I have:
---
title: "Test"
author: "..."
date: "`r Sys.Date()`"
output:
html_document:
includes:
in_header: scripts/uncover.html
...
---
In knitr setup I have:
```{r setup, include=FALSE}
uncover <- function(before, options, envir) {
if (before) {
id <- options$id
button_string <- paste0("<button onclick=\"uncover('",
id,
"')\">Solução</button>")
div_string <- paste0("<div id = '", id,
"', style = 'display:none'>")
paste(button_string, div_string, sep= "\n")
}
else {
"</div><br>"
}
}
And in script uncover.html I have:
<script>
function uncover(id) {
var x = document.getElementById(id);
if (x.style.display === "block") {
x.style.display = "none";
} else {
x.style.display = "block";
}
}
</script>