Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally display a block of text in R Markdown

I am using knitr to parse an R Markdown document . Is there a way to conditionally display a block of text in R Markdown depending on a variable in the environment I pass into knitr?

For instance, something like:

`r if(show.text) {`
  la la la
`r }`

Would print "la la la" in the resulting doc if show.text is true.

like image 801
andrew Avatar asked Aug 20 '14 14:08

andrew


People also ask

How do I insert a code block in R Markdown?

You can insert an R code chunk either using the RStudio toolbar (the Insert button) or the keyboard shortcut Ctrl + Alt + I ( Cmd + Option + I on macOS).

What does ## do in R Markdown?

For example, # Say Hello to markdown . A single hashtag creates a first level header. Two hashtags, ## , creates a second level header, and so on. italicized and bold text - Surround italicized text with asterisks, like this *without realizing it* .

Can R Markdown be interactive?

They will work in any R Markdown format that is viewed in a web browser, such as html documents, notebooks and websites, as well as dashboards and slide presentations. If you include an interactive element in a static output format, like a PDF, R Markdown will embed a screenshot of the element.

Can you highlight in R Markdown?

Markdown - Text highlightBy default, markdown does not support text highlight content. There are multiple ways we can do it using HTML.


4 Answers

You need a complete R expression, so you cannot break it into multiple blocks like you show, but if the results of a block are a text string then it will be included as is (without quotes), so you should be able to do something like:

`r if(show.text){"la la la"}`

and it will include the text if and only if show.text is TRUE.

like image 132
Greg Snow Avatar answered Oct 12 '22 03:10

Greg Snow


You can do this using the "eval" chunk option. See http://yihui.name/knitr/options/.

```{r setup, echo=FALSE}
show_text <- FALSE
````

```{r conditional_block, eval=show_text}
print("this will only print when show.text is TRUE")
```

I've been using YAML config files to parameterize my markdown reports which makes them more reusable.

```{r load_config}
library(yaml)
config <- yaml.load_file("config.yaml")
```

...

```{r conditional_print, eval=config$show_text}
print("la la la")
````
like image 21
paul-boardman Avatar answered Oct 12 '22 04:10

paul-boardman


I find it easiest to do this by putting all of my text into a separate file and then include it from the main file with:

```{r conditional_print, child='text.Rmd', eval = show_text}
```

This has the advantage that you can still put inline R statements or other chunks into the child file, so that if you change your mind about what counts as optional text, you don't have to refactor your project.

like image 42
jkeirstead Avatar answered Oct 12 '22 05:10

jkeirstead


Here's a tweak to Paul Boardman's approach that gives proper markup in the output.

```{r setup, echo=FALSE}
show_text <- FALSE
```

```{r conditional_block, echo=FALSE, results='asis', eval=show_text}
cat("## Hey look, a heading!

lorem ipsum dolor emet...")
```

Even better, if we invoke the python engine to generate our output, we can use triple quoting to easily handle text that contains single or double quotes without needing to do anything fancy:

```{python, conditional_block_py, echo=FALSE, results='asis', eval=show_cond_text}
print("""
## Still a heading

Block of text with 'single quotes' and "double quotes"
""")
```
like image 38
David Marx Avatar answered Oct 12 '22 03:10

David Marx