Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 966
Os2015 Avatar asked May 21 '15 12:05

Os2015


1 Answers

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!"
```
like image 143
Jonathan Avatar answered Sep 18 '22 23:09

Jonathan