Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding code in jupiter in R

I would like to try to produce reports (pdf and probably html) in jupyter using R kernel. However, I would like to hide code in two ways, depending on audience:

  1. all code cells
  2. some code cells

When I have looked for this I found answers for python kernel. Is there a way to this in R (no python code)?

like image 637
Bartek Avatar asked Dec 25 '22 09:12

Bartek


2 Answers

So I have started to combine python's answer: How to hide code from cells in ipython notebook visualized with nbviewer? with How to render LaTeX / HTML in Jupyter (R)? and it works. If one puts the following code in a cell, will get a button for hiding code. From here I think I know where to start.

library(IRdisplay)

display_html(
'<script>  
code_show=true; 
function code_toggle() {
  if (code_show){
    $(\'div.input\').hide();
  } else {
    $(\'div.input\').show();
  }
  code_show = !code_show
}  
$( document ).ready(code_toggle);
</script>
  <form action="javascript:code_toggle()">
    <input type="submit" value="Click here to toggle on/off the raw code.">
 </form>'
)
like image 151
Bartek Avatar answered Jan 07 '23 21:01

Bartek


As you're talking about making HTML and PDF reports, you can do this by using a custom template with nbconvert, and using that to hide cells. This will work for any notebook, independent of the kernel you use.

Docs on nbconvert templates: http://nbconvert.readthedocs.org/en/latest/customizing.html

Examples including hiding code cells based on cell metadata: https://github.com/jupyter/ngcm-tutorial/tree/master/Day-2/nbconvert_templates

like image 20
Thomas K Avatar answered Jan 07 '23 20:01

Thomas K