0

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>

2 Answers2

0

Found a solution here. RMarkdown is friendly with HTML and will allow raw HTML in Rmarkdown as long as it's used properly. I didn't use your uncover.html file, because I used other HTML code directly inside Rmarkdown.

---
title: "Test"
author: "..."
date:  "`r Sys.Date()`" 
output: 
  html_document:
    includes:
      in_header: uncover.html
---
<button class="btn btn-primary" data-toggle="collapse" data-target="#BlockName"> Show/Hide </button>  
<div id="BlockName" class="collapse">  


```{r}

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>"
     }
 }

```

</div>
Daniel_j_iii
  • 3,041
  • 2
  • 11
  • 27
0

I know it has been quite some time, but as I stumbled upon the excellent answer from Daniel when I tried to solve the same problem, I wanted to contribute my adapted solution.

By adding the following code to a code chunk at the beginning of the R markdown document, a new code chunk option called button is added. If it is set, the code chunk is hidden behind a button with a name equal to the value of the button code chunk option.

local({
  hook_chunk <- knitr::knit_hooks$get('chunk')
  knitr::knit_hooks$set(chunk = function(x, options) {
    if (!is.null(options$button)) {
      rnd_id <- substr(tempfile("b", "", ""), 2, 9)
      paste0(
        '<button class="btn btn-primary" data-toggle="collapse" data-target="#',
        rnd_id, '">',
        options$button, '</button> <div id="',
        rnd_id, '" class="collapse">',
        x, '</div>')
    } else {
      x
    }
  })
})

NAME is the name of the button and ID needs to be a unique identifier. The function therefor generates a random ID for every individual code chunk. I know that this is by no means a good solution… but it works.

The function is a modification of a function in the R Markdown Cookbook section 12.1 Redact Source Code. To achieve the desired effect, the knitr chunk hook is redefined to paste the necessary raw HTML code above and below the actual code chunk if the button option is set.

Draugvorn
  • 1
  • 1
  • 1