【发布时间】:2016-05-03 03:22:18
【问题描述】:
我想尝试使用 R 内核在 jupyter 中生成报告(pdf 和可能的 html)。但是,我想以两种方式隐藏代码,具体取决于受众:
- 所有代码单元
- 一些代码单元
当我寻找这个时,我找到了 python 内核的答案。在 R 中有没有办法做到这一点(没有 python 代码)?
【问题讨论】:
标签: r jupyter jupyter-notebook jupyter-irkernel
我想尝试使用 R 内核在 jupyter 中生成报告(pdf 和可能的 html)。但是,我想以两种方式隐藏代码,具体取决于受众:
当我寻找这个时,我找到了 python 内核的答案。在 R 中有没有办法做到这一点(没有 python 代码)?
【问题讨论】:
标签: r jupyter jupyter-notebook jupyter-irkernel
所以我开始结合python的答案: How to hide code from cells in ipython notebook visualized with nbviewer? 和 How to render LaTeX / HTML in Jupyter (R)? 它有效。 如果将以下代码放入单元格中,将获得一个隐藏代码的按钮。从这里我想我知道从哪里开始。
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>'
)
【讨论】:
当您谈论制作 HTML 和 PDF 报告时,您可以通过使用带有 nbconvert 的自定义模板并使用它来隐藏单元格来做到这一点。这适用于任何笔记本,与您使用的内核无关。
关于 nbconvert 模板的文档:http://nbconvert.readthedocs.org/en/latest/customizing.html
包括基于单元元数据隐藏代码单元的示例:https://github.com/jupyter/ngcm-tutorial/tree/master/Day-2/nbconvert_templates
【讨论】:
在第一个单元格中添加一些 CSS 代码,将该单元格更改为“原始 NBConvert”,CSS 中指定的格式将应用于生成的 HTML:
隐藏输入块:
<style type="text/css">
.input_hidden{
display: none
}
</style>
其他样式定义也可以去那里。
然后运行ipython nbconvert the_name_of_the_stuff.ipynb --to slides 以生成 HTML(没有输入块)。
【讨论】: