1

I have an R Markdown file that I want to use as a notebook inside RStudio by interactively executing individual (!) code chunks and seeing their results, e.g., prints, tables, plots, displayed under the code chunk. In particular, I am not interested in knitting the entire R Markdown file at this point. The .Rmd file contains nothing but the following code chunks A, B, and C.

```{r A}
options("width")    # => 96 on my system
```

```{r B}
options(width = 60)
options("width")    # => 60
```

```{r C}
options("width")    # => 96 but I expected 60
```

When I open a fresh instance of RStudio, open the .Rmd file, clear the knitr cache using RStudio's "Clear Knitr Cache..." Knit-menu item, and run the chunks, one by one, in the listed order using RStudio's chunk-specific "Run Current Chunk" buttons, I get 96, the default width on my system, for chunk A, 60 for chunk B, and 96 for chunk C.

Why does the new global width set in chunk B not persist into chunk C?

I am using RStudio version 1.4.1717 with rmarkdown version 2.10 and knitr version 1.34 on macOS Catalina.

A similar question about options(digits = N) was asked here and the single answer was accepted by the asker. However, the example given in the answer fails to demonstrate persistence of R's global options across chunks as the relevant code chunks include their own options(digits = ...) calls. When I tested the example from that answer on my system using an additional code chunk without options(digits = ...) call, the "digits" value set in the previous chunk did not persist.

cbaumbach
  • 13
  • 4
  • I can't reproduce this on Ubuntu 20.04.3 LTS, the `{r C}` shows `60` as expected. Perhaps check your `/usr/lib/R/etc/Rprofile.site` (or wherever it's findable on your machine) file for cross-shooting entries. – jay.sf Sep 12 '21 at 12:54
  • This only happens in RStudio; if you knit the whole document, you should see the expected behaviour. I suspect the reason is that RStudio sets the option depending on the size of your edit window. – user2554330 Sep 12 '21 at 13:04
  • Yes @user2554330 is right, of course you need to knit the whole document! Running `{r B}` line-wise BTW also has no effect. – jay.sf Sep 12 '21 at 13:08

2 Answers2

1

This only happens when using the document as a Notebook in RStudio. If you knit the whole document, you should see the expected behaviour.

I suspect the reason is that RStudio sets the option depending on the size of your edit window. I don't see any option in it that would let you change this behaviour, but you might be able to find one if you look at the .rs.rnb.* objects in the tools:rstudio environment. That's initially loaded in position 2 in the search() list.

user2554330
  • 37,248
  • 4
  • 43
  • 90
  • Indeed, when knitting the entire document the output is as expected. Does this mean that I cannot rely on the results of consecutive code chunk evaluation in RStudio? For me as an emacs/ESS user accustomed to interactive development with a quick feedback loop, this would severely limit the usefulness of RStudio whose R Markdown integration is the only selling point from where I stand. I read that chunk caching can speed things up but I am reluctant to introduce another level of complexity and source of errors. I am a first-time R Markdown user. Am I missing something? – cbaumbach Sep 12 '21 at 13:44
  • If your computations depend on `options("width")`, then for sure they will be different in the notebook versus the full run. I don't know what other options would change, but in my experience, executing one chunk at a time is very error prone: many code chunks depend on earlier ones being executed just once in the correct order. These are errors in my programming, nothing to do with RStudio per se. I don't use RStudio notebooks for this reason and others. – user2554330 Sep 12 '21 at 13:57
0

I'm not sure what you are knitting to, but I had similar issues trying to keep the text when knitting to a markdown file from wrapping with a hard cut-off of 60, and setting the options(width = XXX) never seemed to work for me.

Instead, in the YAML header you can tell pandoc not to wrap, but rather preserve the text as you have typed it in the .Rmd.

output:
  md_document:
    pandoc_args: ["--wrap=preserve"]

Again, not sure if you are knitting to an .md file, but this might be useful.