Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Respecting global options in knitr

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

like image 339
sinjin Avatar asked Feb 24 '16 15:02

sinjin


People also ask

What is the purpose of knitr?

knitr is an engine for dynamic report generation with R. It is a package in the programming language R that enables integration of R code into LaTeX, LyX, HTML, Markdown, AsciiDoc, and reStructuredText documents. The purpose of knitr is to allow reproducible research in R through the means of literate programming.

What does knitr :: Opts_chunk set echo true mean?

The first code chunk: ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ``` is used to specify any global settings to be applied to the R Markdown script. The example sets all code chunks as “echo=TRUE”, meaning they will be included in the final rendered version.

What are the three types of sections in an R Markdown document select three options?

The following sections dive into the three components of an R Markdown document in more details: the markdown text, the code chunks, and the YAML header.

What does the chunk option message do?

Chunk Options include = FALSE prevents code and results from appearing in the finished file. R Markdown still runs the code in the chunk, and the results can be used by other chunks. echo = FALSE prevents code, but not the results from appearing in the finished file. This is a useful way to embed figures.


1 Answers

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.

like image 105
Yihui Xie Avatar answered Nov 15 '22 06:11

Yihui Xie