Hatshepsut
Hatshepsut

Reputation: 6662

How to evaluate all chunks in Rmarkdown

How do I evaluate all of the chunks in an Rmd document, without putting eval=TRUE at each chunk? The way I have it below, only the first chunk is evaluated.

```{r,eval=TRUE}
1+1
```

Some text

```
2+2
```

EDIT:

I'm trying to knit/compile to HTML.

```
require(knitr)
opts_chunk$set(eval = TRUE, tidy = TRUE, cache = FALSE, echo = FALSE, include = FALSE,
               fig.path = 'figures/', dev = c("pdf"),
               fig.width = 7, fig.height = 7)
```
some text

```
1+1
```
more text
```
2+2
```

Upvotes: 3

Views: 1637

Answers (2)

jbaums
jbaums

Reputation: 27398

eval=TRUE is the default behaviour for .Rmd chunks, so you shouldn't need to explicitly add it to your chunks' options.

However, you do need to include {r} after your opening fences in order for the chunk to be recognised as R code and evaluated accordingly. Chunks that do not open with ```{r} will not be run, hence the problem you're seeing.

A working example might be:

```{r}
1+1
```
Some text

```{r}
2+2
```

To insert a new, empty chunk with the appropriate fences and {r}, you can press Ctrl + Alt+i on Windows, or + Option + i on Mac, or click this icon at the top right of the RStudio source pane (from memory, older versions of RStudio had an 'Insert' drop-down in that general area):

enter image description here

Upvotes: 4

Bryan Hanson
Bryan Hanson

Reputation: 6223

In your first chunk you can set knitr options globally.

opts_chunk$set(tidy = TRUE, cache = FALSE, echo = FALSE, include = FALSE,
    fig.path = 'figures/', dev = c("pdf"),
    fig.width = 7, fig.height = 7)

In any subsequent chunk, you can change these by the usual means but they only apply to that chunk.

EDIT. Here is a fuller example from K. Broman

```{r global_options, include=FALSE}
knitr::opts_chunk$set(fig.width=12, fig.height=8, fig.path='Figs/',
                      echo=FALSE, warning=FALSE, message=FALSE)
```

Upvotes: 1

Related Questions