在R markdown中折叠/隐藏数字

时间:2017-03-06 16:07:16

标签: r r-markdown

在我的rmarkdown文档中,我可以使用以下内容显示和隐藏代码 - 这会在每个代码块之前在文档的右侧创建一个方便的按钮:

output: 
  html_document:
    code_folding: hide

enter image description here enter image description here

是否有类似方便的隐藏表格或数字的方法?如果是这样,请提供参考,因为我找不到任何参考。否则,我们将不胜感激,谢谢!

4 个答案:

答案 0 :(得分:14)

我无法让上述解决方案(或我发现的其他解决方案)始终如一地工作,但使用我在W3schools.com找到的内联html(Bootstrap示例/解决方案)在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>

答案 1 :(得分: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>

然后在每个块之前要进行切换:

<input type=button class=hideshow></input>

(改编自:https://groups.google.com/forum/#!topic/knitr/d37E0mP3w6k

注意:如果您显示代码,这将有效 - 如果您隐藏代码(使用echo = FALSE),请更改

target = target.nextSibling.nextSibling.nextSibling.nextSibling;

target = target.nextSibling.nextSibling;

注2:如果您想使用code_folding选项,请更改

 target = target.nextSibling.nextSibling.nextSibling.nextSibling;

 target = target.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling;

答案 2 :(得分:0)

我能够让Lucy的代码为我工作,但是我还认为默认情况下隐藏输出会更有用和更清洁。这是一个非常简单的补充。只需执行渲染时单击所有“隐藏输出”按钮的jquery。我的代码如下:

<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;
    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';
    }
  } );
} );

$("input.hideshow").click()
</script>

只有</script>之前的最后一行是加法。

答案 3 :(得分:0)

嘿,所以我更改了gclarkjr5的代码,使表显示时未格式化。 它将首先显示隐藏的按钮,而不修改表的显示。 所以去了:

将其放在要显示的块之前:

emitRecordWithTimestamp

然后在标记文件的末尾:

<input type=button class=hideshow></input>