Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Markdown code folding doesn't work with bash, Python code chunks

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?

like image 459
user5359531 Avatar asked Oct 12 '16 19:10

user5359531


People also ask

How do I enable code folding in R?

Menu Commands and ShortcutsEdit -> Folding: Collapse — Alt+L. Expand — Shift+Alt+L. Collapse All — Alt+O.

How do I hide chunks of code in R Markdown?

Chunk options The initial line in a code chunk may include various options. For example, echo=FALSE indicates that the code will not be shown in the final document (though any results/output would still be displayed). You use results="hide" to hide the results/output (but here the code would still be displayed).

How do I add code chunks in R Markdown?

You can insert an R code chunk either using the RStudio toolbar (the Insert button) or the keyboard shortcut Ctrl + Alt + I ( Cmd + Option + I on macOS). There are a large number of chunk options in knitr documented at https://yihui.name/knitr/options.

Can you use R Markdown with Python?

The reticulate package includes a Python engine for R Markdown that enables easy interoperability between Python and R chunks.


2 Answers

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"
```
like image 191
Oo.oO Avatar answered Nov 03 '22 18:11

Oo.oO


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.

like image 39
arpa583 Avatar answered Nov 03 '22 18:11

arpa583