3

Using the chunk option eval=FALSE one can suppress chunk evaluation in an RMarkdown file or R Notebook when it is knit. Is there a way to make this apply during interactive running of the document in RStudio (i.e., making "run all chunks" skip certain chunks)?

I've got some chunks in the beginning of my analysis that take a while to run, which the later sections don't depend on. I want to be able to source the important parts of the code so I can continue writing the downstream stuff, without having to do it manually chunk-by-chunk so I can avoid the parts I don't need in my workspace for further writing.

I've set up the rmarkdown document with logical parameters meant to change which parts of the code need to get run - I meant these as control flags for when the code is actually finished and getting used, but I was hoping I could use those same parameters to exclude chunks from running in interactive mode (i.e., something like eval=params$run_part1).

Empiromancer
  • 3,778
  • 1
  • 22
  • 53

1 Answers1

3

Setting knitr::opts_chunk and knitr::opts_hooks only help you when knitting, not in interactive mode, so while I could be wrong I'm going to tentatively say you can't control that behavior with dynamic chunk options (yet).

As a workaround you could use interactive() and if blocks so that the code is only run when knitted. It would also mesh well with your logical parameters, despite the pain of having to be in a bracket block.

---
title: "R Notebook"
output:
  html_document: default
  html_notebook: default
---

```{r}
if (!interactive()) {
  print("long running code")
}
```

```{r}
print(2)
```

```{r}
print(3)
```

Pressing "Run All Chunks Above":

enter image description here

Knitting:

enter image description here

mlegge
  • 6,763
  • 3
  • 40
  • 67
  • The ability to distinguish interactive is great. Now I can get away from the annoyance of failed run while writing a notebook. Thank you. – KenM Aug 10 '20 at 12:44