【问题标题】:Knitr: How to hide knitr::kable() tables to be correctly shown afterwards in a html document?Knitr:如何隐藏 knitr::kable() 表以便之后在 html 文档中正确显示?
【发布时间】:2016-04-18 12:29:24
【问题描述】:

我试图隐藏格式正确的 knitr::kable() 表。不隐藏它们看起来不错......但如果我显示已经隐藏的表格,它们会显示为普通文本......到目前为止这是我的代码:

---
title: "Test-Tabellen"
output: 
  html_document:
    code_folding: hide
---

<div id="BE_Genus_C.txt" style="display:none">
```{r cache=FALSE, collapse=TRUE, results='asis'}
test<-read.csv2("BE_Genus_C.txt", header=FALSE, sep="\t")
knitr::kable(test)
```
</div>

<button title="Click to show answer" type="button" onclick="if(document.getElementById('BE_Genus_C.txt') .style.display=='none') {document.getElementById('BE_Genus_C.txt') .style.display=''}else{document.getElementById('BE_Genus_C.txt') .style.display='none'}">Show/hide</button>

您知道如何以另一种方式隐藏使用 knitr::kable() 生成的表格吗?或者至少正确显示它们?

我为什么需要它的问题如下:我有大约 100 张小桌子,我不想直接看到它们……但我希望能够在需要时查看它们…… ..我也想把表格(附上一些解释)寄给我的主管。

【问题讨论】:

  • div 标签和 R 代码块之间添加一个额外的换行符。
  • 非常感谢!它对我有用:) 如果您再次将此添加为答案,我可以根据需要将其检查为“对我有帮助”(绿色标志)。我测试了一下:只需要在一个chunk结束后进行换行即可。

标签: html r knitr


【解决方案1】:

这是因为pandoc tables should end with a blank line,所以在R代码块和关闭div标签之间添加一个额外的行:

<div id="BE_Genus_C.txt" style="display:none">
```{r cache=FALSE, collapse=TRUE, results='asis'}
knitr::kable(head(iris))
```

</div>

<button title="Click to show answer" type="button" onclick="if(document.getElementById('BE_Genus_C.txt') .style.display=='none') {document.getElementById('BE_Genus_C.txt') .style.display=''}else{document.getElementById('BE_Genus_C.txt') .style.display='none'}">Show/hide</button>

下次别忘了使用可重现的例子(例如iris vs read.csv):)

【讨论】:

  • @TheGreenOne 我突然想到我碰巧维护了一个将 R 对象转换为降价的 R 包,名为 pander,它自动将额外的换行符添加到表格的末尾,所以解决您的问题的另一种方法是将knitr::kintr 替换为pander::pander,而不是额外的换行符。
猜你喜欢
  • 1970-01-01
  • 2018-04-20
  • 2015-03-23
  • 1970-01-01
  • 1970-01-01
  • 2019-04-08
  • 2015-11-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多