4

I am working on an Rmd down report and depending on an R variable I want to decide whether to include a paragraph or not

e.g.

##Abstract
paragraph Blurb

If result type is 1 then
another paragraph of blurb

I can't find any easy way to do this. I've tried using a code chunk.

e.g

```{r echo=FALSE}
    if ( resultType1 ) {
        cat(c("lines of blurb","more lines of blurb"))
    }
```

Unfortunately, this outputs the optional paragraph in a box plus in a totally different font from the general abstract paragraph and has a feel of surely there is a better way to do this

Phil
  • 7,287
  • 3
  • 36
  • 66
Gordon
  • 73
  • 7

2 Answers2

7

what about using results='asis' in the code chunk header.

```{r, echo=FALSE, results='asis'}
if ( resultType1 ) {
  cat(c("lines of blurb","more lines of blurb"))
}
```

It is also possible to print headlines with ## etc.

drmariod
  • 11,106
  • 16
  • 64
  • 110
0

Using an R chunk and cat() does not work when the text has some LaTeX commands.

Instead, and courtesy of [this solution][1] you can simply use the asis engine directly, e.g.:

```{asis, echo=resultType1}
lines of blurb
more lines of blurb
even more lines of blurb including an equation: $\hat{y} = \hat{\beta_0} + \hat{\beta_1}X_1$

  [1]: https://github.com/rstudio/rmarkdown-cookbook/issues/113