Reputation: 10984
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?
Upvotes: 100
Views: 183350
Reputation: 15419
Just aggregating the answers and expanding on the basics. Here are three options:
We can include echo=FALSE
in the chunk header:
```{r echo=FALSE}
plot(cars)
```
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)
```
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)
```
Upvotes: 56
Reputation: 759
Alternatively, you can also parse a standard markdown document (without code blocks per se) on the fly by the markdownreports package.
Upvotes: 1
Reputation: 1320
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)
Upvotes: 57
Reputation: 368519
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.
Upvotes: 87