6

R Markdown now has the option to automatically show or hide code chunks in your .Rmd document. However, this seems to only work with R code chunks.

---
output: 
  html_document: 
    code_folding: hide
---

```{r}
print("This code chunk will be hidden")
```

```{r, engine='bash'}
echo "This code chunk will not be hidden"


```{r, engine='python'}
print "Will this code chunk be hidden?"
```

```{r}
system('uname -srv',intern=T)
sessionInfo()
```

Output

The only solution I have been able to come up with is to hide the code behind a blank tab

---
output: 
  html_document: 
    code_folding: hide
---

```{r}
print("This code chunk will be hidden")
```

# Source code {.tabset .tabset-pills}

## Hide Code

## Show Code

```{r, engine='bash'}
echo "This code chunk will not be hidden"
```

```{r, engine='python'}
print "Will this code chunk be hidden?"
```

```{r}
system('uname -srv',intern=T)
sessionInfo()
```

Blank Tab

Is there a better solution to this which will enable code folding for all code chunks?

user5359531
  • 3,217
  • 6
  • 30
  • 55
  • Is _code_folding: hide_ equivalent to setting `knitr::opts_chunk$set(echo = FALSE)` on initialization? – lukeA Oct 12 '16 at 21:17
  • 1
    No. 'code folding' is a distinct feature. The `knitr` option you mention prevents all (or selected) code chunks from appearing at all in the document. Code folding instead places a small button labeled "code" in the document, and clicking on it will show or hide the code chunk. This allows the end user to toggle code visibility in the compiled document. – user5359531 Oct 12 '16 at 21:42
  • in essence, knitr options come into effect when the .Rmd file is compiled to .md. This, and other features embedded in the YAML file header, come into effect when the .md file is compiled by pandoc into HTML. – user5359531 Oct 12 '16 at 22:06
  • 3
    You might need to adapt the code a bit but maybe this helps: http://stackoverflow.com/questions/37755037/how-to-add-code-folding-to-output-chunks-in-rmarkdown-html-documents/37839683#37839683 – Martin Schmelzer Oct 13 '16 at 09:20
  • @MartinDabbelJuSmelter Thanks a lot for pointing that out, I will look into it. Coincidentally, figuring out how to write and embed my own custom JavaScript in my RMarkdown was next on my to-do list. – user5359531 Oct 13 '16 at 15:36

2 Answers2

2

Maybe R version plays a role here? For me, without any modifications, your code works as expected:

## R version 3.3.3 (2017-03-06)
## Platform: x86_64-apple-darwin13.4.0 (64-bit)
## Running under: macOS Sierra 10.12.6

as for R studio

Version 1.0.136 – © 2009-2016 RStudio, Inc.

enter image description here

Of course, I have changed (from your initial post)

```{r, engine='bash'}
echo "This code chunk will not be hidden"

with

```{r, engine='bash'}
echo "This code chunk will not be hidden"
```
Oo.oO
  • 12,464
  • 3
  • 23
  • 45
2

You can fix it in post-production too. I convert Rmd->HTML using rmarkdown::render() (R 3.4.1) and pandoc-1.17.2. The resulting HTML file uses different CSS classes for different languages, but only the <pre> class "sourceCode r" supports folding.

So, just change all non-"r" classes to "r" in the <pre> tags:

perl -i -pe 's/<pre class="sourceCode [^r]+">/<pre class="sourceCode r">/' myfile.html

The appearance of the code in the blocks will not change.

arpa583
  • 21
  • 1