TheGreenOne
TheGreenOne

Reputation: 13

Knitr: How to hide knitr::kable() tables to be correctly shown afterwards in a html document?

I was trying to hide correctly formatted knitr::kable() tables. Without hiding they are looking nice... but if I am showing already hidden tables they appear as normal text.... that's my code so far:

---
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>

Do you have any idea how I could hide my tables generated with knitr::kable() in another way? Or at least show them correctly?

My problem why I need that is the following: I have about 100 small tables which I don't want to see directly... but I would like to be able to have a look to them whenever I need them.... and I also would like to send the tables (with some explainations) to my supervisor.

Upvotes: 1

Views: 1216

Answers (1)

daroczig
daroczig

Reputation: 28672

This happens because pandoc tables should end with a blank line, so add an extra-line between the R code chunk and closing div tag:

<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>

And don't forget to use a reproducible example (eg iris vs read.csv) next time :)

Upvotes: 1

Related Questions