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)
```
opts_chunk$set(echo=FALSE)
in a code chunk at the head of your document to set this globally – Rhapsodic