Evaluate a Chunk based on the output format of knitr

我的梦境 提交于 2019-12-21 19:01:31

问题


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


回答1:


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!"
```


来源:https://stackoverflow.com/questions/30374492/evaluate-a-chunk-based-on-the-output-format-of-knitr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!