Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include a header based on a condition in knitr

Tags:

r

knitr

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)
 }
```
like image 484
ah25 Avatar asked Feb 23 '16 23:02

ah25


People also ask

How do you make a header in R markdown?

Creating Headings and Subheadings We can insert headings and subheadings in R Markdown using the pound sign # . There are six heading/subheading sizes in R Markdown. The number of pound signs before your line of text determines the heading size, 1 being the largest heading and 6 being the smallest.

What does knitr :: Opts_chunk set echo true mean?

The first code chunk: ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ``` is used to specify any global settings to be applied to the R Markdown script. The example sets all code chunks as “echo=TRUE”, meaning they will be included in the final rendered version.

How do you use knitr in R?

There are two ways to render an R Markdown document into its final output format. If you are using RStudio, then the “Knit” button (Ctrl+Shift+K) will render the document and display a preview of it. Note that both methods use the same mechanism; RStudio's “Knit” button calls rmarkdown::render() under the hood.

What is the function of knitr in creating markdown document?

knitr is an engine for dynamic report generation with R. It is a package in the programming language R that enables integration of R code into LaTeX, LyX, HTML, Markdown, AsciiDoc, and reStructuredText documents. The purpose of knitr is to allow reproducible research in R through the means of literate programming.


1 Answers

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.

like image 144
CL. Avatar answered Sep 28 '22 16:09

CL.