8

I am having trouble with setting global options in my R Markdown documents. A simple example follows. In this case I've attempted to set global.par = TRUE. The expectation is that any par() specifications I make in one chuck are carried into subsequent chunks. However, this is not happening.

```{r package_options, include=FALSE}
knitr::opts_knit$set(global.par = TRUE)
```

```{r}
lambda <- 0.2; n <- 1000
exp <- rexp(n, lambda)
par(cex = 0.7)
hist(exp)
```

```{r}
lambda <- 0.02; n <- 1000
exp <- rexp(n, lambda)
hist(exp)
```

Specs: Max OS 10.11, R version 3.2.0 (2015-04-16), RStudio 0.98.1062, knitr 1.12.3

CL.
  • 14,577
  • 5
  • 46
  • 73
sinjin
  • 155
  • 5

1 Answers1

6

This issue has been fixed in knitr (>= v1.12.17), and you may test the current development version of knitr on Github. I just discovered that setting mfcol/mfrow will reset cex to 1, and that was the root cause of knitr not being able to restore cex correctly.

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • Confirmed, that fixes the bug. But is it really a good idea to [prohibit changing `usr`](https://github.com/yihui/knitr/commit/24344832b8b352bac0a97cca54b4ccc6b4232d60#diff-04f9cbb0662e7b2fe9b767a43c0e7d8cR332)? Who sets this parameter should be aware of that he/she is doing. And if they want to shoot themselves in the foot ... – CL. Mar 05 '16 at 12:07
  • 1
    As usual, you know these details in knitr very well. However, the problem is that there is no way to detect if the user has intentionally changed `par(usr)` in a code chunk. `usr` is always set to new values whenever you draw new plots. Try to run `str(par('usr')); plot(1:10); str(par('usr')); plot(rnorm(100)); str(par('usr'))` and you will see it. I think it should be rare that users want to set `usr`, so I just removed it from the parameters. – Yihui Xie Mar 05 '16 at 20:55