Matthew Law
Matthew Law

Reputation: 2361

Include code for plots in an R Notebook output

This feels like it should be really straightforward, but I can't work out how to include the code used to create a plot in an R Notebook (i.e. above the plot it creates). Confusingly, some online examples (eg the one immediately below, from here) show the plot code being included automatically in the html_notebook output, but other sources (eg the bottom image, from here show the code for the plot being hidden (which is the behaviour I am getting at the moment). I am aware that the bottom example is a html_document output and not a html_notebook and that there are differences between the two, but in any case I am using a html_notebook but seeing the behaviour in the second image.

I have tried the different chunk options listed here and here (eg echo=TRUE) but to no avail, so any help would be greatly appreciated.

example showing what I want

example showing what I get at the moment

Upvotes: 2

Views: 862

Answers (1)

Waldi
Waldi

Reputation: 41240

The first document uses the code_folding: show YAML option with chunck option echo = T:

---
title: "Notebook"
output:
  html_notebook:
    code_folding: show
  html_document:
    code_folding: show
---

enter image description here

The second document just uses global chunk option echo = F:

---
title: "Notebook"
output:
  html_document
---

`r knitr::opts_chunk$set(echo = F)`

```{r}
plot(1)
```

enter image description here

Upvotes: 1

Related Questions