2

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

Phil
  • 7,287
  • 3
  • 36
  • 66
Matthew Law
  • 2,293
  • 2
  • 13
  • 16

1 Answers1

1

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

Waldi
  • 39,242
  • 6
  • 30
  • 78
  • Thanks – do you know if there is a way to get this to work for `output: html_notebook` (not `html_document`)? I can get it to work for [document](https://i.imgur.com/iwLX6oX.png), but not for [notebook](https://i.imgur.com/3iDnSfn.png), which is what I want. – Matthew Law Jan 30 '21 at 09:14
  • 1
    See my edit : I added the same YAML for `html_notebook` and `html_document` and it now seems to work as expected. When just setting `html_notebook` YAML, RStudio automatically inserts a new `html_document` YAML at the first `knit to HTML` – Waldi Jan 30 '21 at 17:27
  • Thanks – I've copied your YAML but I'm still getting the same behaviour as before (like [this](https://i.imgur.com/wxCYCfA.png)) – do you know what I need to change? – Matthew Law Jan 30 '21 at 18:19
  • 1
    What happens if you knit to html? You also don't seem to be using the latest [RStudio 1.4 version](https://rstudio.com/products/rstudio/download/#download) – Waldi Jan 30 '21 at 18:53
  • Just tried and it works if I knit it (so to a `html_document`), but not if I preview the notebook (so to a `html_notebook`). Maybe this is just a feature, but it would be nice to be able to save it to a .nb.html but still show the plot code above the plots. – Matthew Law Jan 30 '21 at 20:46
  • AFAIK, preview uses data from previous knit. So after knitting once, preview shows code + plot in my case. – Waldi Jan 30 '21 at 22:34
  • Hmm, for me I can knit it and get the code + plot, but as soon as I switch to preview the code disappears. I've updated my RStudio version so I don't think it's that. – Matthew Law Jan 31 '21 at 09:48
  • 1
    We're missing something here, and I stuck ;) – Waldi Jan 31 '21 at 09:53
  • Thanks for trying to help in any case, it's not the end of the world if I use `html_document` instead (or don't see the code for the plots) – Matthew Law Jan 31 '21 at 10:54