如何在knitr中基于条件包括标头 [英] How to include a header based on a condition in knitr

查看:61
本文介绍了如何在knitr中基于条件包括标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Rmd文件中有一个标头,后跟一个代码块.如果满足条件,我只想包含此标头和其后的块.我知道如何使用代码块,因为它位于代码的主体中,但是我该如何做呢?

I have a header followed by a code chunk in an Rmd file. I only want to include this header and the chunk followed by it, if a condition is met. I know how to do that with the chunk, since it's in the body of the code, but how do I do the former?

```{r}
 print_option <- TRUE
```

## My header
```{r}
 if(print_option==TRUE) {
 print (x)
 }
```

推荐答案

我知道了:)

    ```{r, echo=FALSE,  include=FALSE}
    x<- FALSE
    if ( x ) {
      Title <- "My header"
    } else {Title=""}
    ```
    ## `r Title` 
    ```{r, echo=FALSE}
    if(x) {
    print(1:10)
    }
    ```

这篇关于如何在knitr中基于条件包括标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆