在R降价中折叠/隐藏数字

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)


Luc*_*ucy 5

如果将其添加到.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)

  • 太好了,谢谢!您是否知道默认情况下如何隐藏它,以便用户可以单击“显示输出”按钮,而不是遇到需要隐藏的庞大表?超级有用的功能,我希望看到它添加到KnitR等中。 (2认同)