7

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)
 }
```
CL.
  • 14,577
  • 5
  • 46
  • 73
asiehh
  • 553
  • 12
  • 22

2 Answers2

14

The chunk option eval and asis_output() provide a simple solution.

Assuming that print_option is a boolean indicating whether to show the header (and whether to execute other code like print(1:10) in chunk example1):

```{r setup}
library(knitr)
print_option <- TRUE
```

```{r, eval = print_option}
asis_output("## My header\\n") # Header that is only shown if print_option == TRUE
print(1:10) # Other stuff that is only executed if print_option == TRUE
```

Text that is shown regardless of `print_option`.

```{r setup2}
print_option <- FALSE
```

Now `print_option` is `FALSE`. Thus, the second header is not shown.

```{r, eval = print_option}
asis_out("## Second header\\n")
```

Output:

Output

For longer conditional outputs (of text/markdown, without embedded R code) the engine asis can be helpful, see this answer (it's long, but the solution at the end is very concise).

Addendum

Why is ## `r Title` with Title set to "My header" or "" as suggested in this answer a bad idea? Because it creates an "empty header" in the second case. This header is not visible in the rendered HTML/markdown output, but it is still there. See the following example:

```{r, echo = FALSE}
title <- ""
```

## `r title`

This generates the following markdown ...

## 

... and HTML:

<h2></h2>

Besids being semantically nonsense it might lead to layout issues (depending on the style sheet) and disrupts the document outline.

Community
  • 1
  • 1
CL.
  • 14,577
  • 5
  • 46
  • 73
  • Thanks @CL. The only problem with your solution is that it will print the rest of the code output in the chunk like text. – asiehh Feb 24 '16 at 18:46
  • @Asieh I suppose you're referring to the linked answer, right? Or is anything wrong with the `cat` solution here? – CL. Feb 24 '16 at 19:13
  • No, I'm referring to your answer, though the linked solution gives the same output. It is about the format of the output code in the chunk that is different from the normal output format. I just wanted to have the option to include the header or not. I did not want to change the output format of the code. – asiehh Feb 24 '16 at 19:16
  • @Asieh thank you for the feedback. I'm writing from my phone at the moment but I will look into this again later when I'm back at the laptop.... – CL. Feb 24 '16 at 19:18
  • @AsiehHarati Updated the answer. If you were referring to the formatting of the output from `print(1:10)`, that's fixed now. Let me know if I'm misunderstanding the question. – CL. Feb 24 '16 at 20:54
0

I figured it out :)

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