title: Toggle chunk output using "details" HTML tag
body: <p>I'm writing some tutorials using blogdown. For a pedagogical reason, I want my students to think before seeing the solution. Here's my current code.</p>
  
  <p><strong>Original</strong></p>
  
  <pre><code>---
  title: "Toggle Chuck Output Using details Tag"
  output: html_document
  ---
  
  ```{r calc, prompt=TRUE, eval=FALSE}
  90 + 30
  ```
  
  &lt;details&gt;
    &lt;summary&gt;Toggle output&lt;/summary&gt;
  ```{r, ref.label='calc', echo=FALSE, prompt=TRUE}
  ```
  &lt;/details&gt;
  </code></pre>
  
  <p>Here's my attempt:     </p>
  
  <p>To avoid repeatedly writing the HTML tags, I think I need to define a function similar to ...  </p>
  
  <pre><code>togglable &lt;- function(label, summary = "Toggle output"){
    cat('&lt;details&gt;')
    cat(' &lt;summary&gt;', summary, '&lt;/summary&gt;', sep = '')
  
    # Code to print output using 'ref.label' should go here.
    # The following doesn't work. 
    knitr::knit_print(knitr:::knit_code$get(label))
  
    cat('&lt;/details&gt;')
  }
  </code></pre>
  
  <p>.... then replace the <code>&lt;detals&gt;...&lt;/details&gt;</code> block with a R code chunk similar to the following: </p>
  
  <p><strong>Use case 1 (better)</strong> </p>
  
  <pre><code>```{r usecase1, echo=FALSE, results='asis'}
  togglable(label = "calc")
  ```
  </code></pre>
  
  <p>I tried to make it work, but in vain.  </p>
  
  <p>One more thing. If possible, I'd like this <code>togglable()</code> function to override the chunk options so that I don't even need to write <code>echo=FALSE</code>, <code>results='asis'</code>, because the following chunk would look nicer.</p>
  
  <p><strong>Use case 2 (best)</strong></p>
  
  <pre><code>```{r usecase2}
  togglable(label = "calc")
  ```
  </code></pre>
  
  <p>In summary, I would like to ask the following questions. </p>
  
  <ol>
  <li>How can I define this <code>togglable()</code> function so that it behaves  in the same way as the original <code>&lt;detals&gt;...&lt;/details&gt;</code> block?</li>
  <li>Is it possible that this function overrides the options (<code>echo</code> and <code>results</code> in particular) for the chunk where this function is called? If yes, how?</li>
  <li>Alternatively, is there any other idea how to produce the result of the original code without repeatedly writing the HTML tags? </li>
  </ol>
  
  <p>Thank you very much!</p>