Hiding the R code in Rmarkdown/knit and just showing the results
Asked Answered
T

4

97

In my R Markdown documents, I sometimes want to just generate a report without showing the actual code (specially when I send it to my boss). How can I hide the R code and just show the graph and results?

For example:

---
output: html_document
---

```{r fig.width=16, fig.height=6}
plot(cars)
```

This shows both the commands and the plot. How can I remove the commands from my HTML report?

Threadbare answered 26/10, 2012 at 16:49 Comment(0)
S
86

Sure, just do

```{r someVar, echo=FALSE}
someVariable
```

to show some (previously computed) variable someVariable. Or run code that prints etc pp.

So for plotting, I have eg

### Impact of choice of ....
```{r somePlot, echo=FALSE}
plotResults(Res, Grid, "some text", "some more text")
```

where the plotting function plotResults is from a local package.

Samualsamuel answered 26/10, 2012 at 16:54 Comment(2)
or put opts_chunk$set(echo=FALSE) in a code chunk at the head of your document to set this globallyRhapsodic
Make sure not to echo the opts_chunk command! Putting {r echo=FALSE} opts_chunk$set(echo=FALSE) at the top of the doc (in a code chunk) should solve everythingTransfix
W
57

Might also be interesting for you to know that you can use:

{r echo=FALSE, results='hide',message=FALSE}
a<-as.numeric(rnorm(100))
hist(a, breaks=24)

to exclude all the commands you give, all the results it spits out and all message info being spit out by R (eg. after library(ggplot) or something)

Wolffish answered 14/12, 2012 at 23:27 Comment(1)
That is nice, results='hide' represses text output, but shows plots!Cryohydrate
E
54

Just aggregating the answers and expanding on the basics. Here are three options:

1) Hide Code (individual chunk)

We can include echo=FALSE in the chunk header:

```{r echo=FALSE}
plot(cars)
```

2) Hide Chunks (globally).

We can change the default behaviour of knitr using the knitr::opts_chunk$set function. We call this at the start of the document and include include=FALSE in the chunk header to suppress any output:

---
output: html_document
---

```{r include = FALSE}
knitr::opts_chunk$set(echo=FALSE)
```

```{r}
plot(cars)
```

3) Collapsed Code Chunks

For HTML outputs, we can use code folding to hide the code in the output file. It will still include the code but can only be seen once a user clicks on this. You can read about this further here.

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


```{r}
plot(cars)
```

enter image description here

Exhilarative answered 1/10, 2018 at 10:25 Comment(1)
Can I exclude printing code when knitting to PDF using an option in the preamble? knitr::opts_chunk$set(echo=FALSE) works, but I would prefer an option like pdf_document code_folding: "hide".Whitneywhitson
D
1

Alternatively, you can also parse a standard markdown document (without code blocks per se) on the fly by the markdownreports package.

Discriminative answered 14/9, 2017 at 12:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.