Os2015
Os2015

Reputation: 73

Evaluate a Chunk based on the output format of knitr

I am a beginner in using Markdown (I am using it with R studio and knitr).

I am struggling with a point that I hope you would be able to help me to figure it out.

I would like a chunk to be evaluated only if the output_format of the rmarkdown document is pdf. In other words, the chunk option "eval" automatically set to TRUE if the knitr output was selected as "knitr PDF". Otherwise, eval = FALSE.

In your view, what is the most straightforward way to do this.

Many thanks in advance

Upvotes: 7

Views: 590

Answers (1)

Jonathan
Jonathan

Reputation: 8822

Try this:

```{r eval = knitr::is_latex_output()}
"Hi, I'm in a PDF!"
```

Or, to evaluate chunks only when you're not rendering to PDF:

```{r eval = !knitr::is_latex_output()}
"Hi, I'm not in a PDF!"
```

Upvotes: 18

Related Questions