the*_*ide 10 r r-markdown
在我的rmarkdown文档中,我可以使用以下内容显示和隐藏代码 - 这会在每个代码块之前在文档的右侧创建一个方便的按钮:
output:
html_document:
code_folding: hide
Run Code Online (Sandbox Code Playgroud)
隐藏表格或数字是否有类似方便的方法?如果是这样,请提供参考,因为我找不到任何参考.否则,我们将不胜感激,谢谢!
Mat*_* L. 15
我一直没能得到上述溶液(或其他人,我发现)工作始终,但使用在线HTML(引导例子/解决方案),我发现在W3schools.com在Rmarkdown效果很好.
我在下面的例子中使用它来显示html输出中的简单绘图.它应该适用于任何块输出:
<button class="btn btn-primary" data-toggle="collapse" data-target="#BlockName"> Show/Hide </button>
<div id="BlockName" class="collapse">
```{r}
plot(mtcars$disp, mtcars$mpg)
```
</div>
Run Code Online (Sandbox Code Playgroud)
如果将其添加到.Rmd文件末尾
<script>
$( "input.hideshow" ).each( function ( index, button ) {
button.value = 'Hide Output';
$( button ).click( function () {
var target = this.nextSibling ? this : this.parentNode;
target = target.nextSibling.nextSibling.nextSibling.nextSibling;
if ( target.style.display == 'block' || target.style.display == '' ) {
target.style.display = 'none';
this.value = 'Show Output';
} else {
target.style.display = 'block';
this.value = 'Hide Output';
}
} );
} );
</script>
Run Code Online (Sandbox Code Playgroud)
然后在你想要切换的每个块之前:
<input type=button class=hideshow></input>
Run Code Online (Sandbox Code Playgroud)
(改编自此处:https://groups.google.com/forum/#!topic/knitr/d37E0mP3w6k)
注意:如果您显示代码,这将有效 - 如果您隐藏代码(带echo = FALSE),请更改
target = target.nextSibling.nextSibling.nextSibling.nextSibling;
Run Code Online (Sandbox Code Playgroud)
至
target = target.nextSibling.nextSibling;
Run Code Online (Sandbox Code Playgroud)
注2:如果要使用该code_folding选项,请更改
target = target.nextSibling.nextSibling.nextSibling.nextSibling;
Run Code Online (Sandbox Code Playgroud)
至
target = target.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling;
Run Code Online (Sandbox Code Playgroud)