0

I would like to conditionally print out text in a Rmarkdown notebook I am writing to automate a report for a project. I have found an answer to conditionally print out text in the rmarkdown notebook using the the code below:

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

Which would print out inline, "la la la" if the show.text object is true. What I would like to know is how to use functions within the brackets, for example:

`r if(show.text){print(x)}`

The code above does not work. I would like to be able to combine this to make a more complicated conditional

`r if(show.text){"la la la" print(x) "bi bi bi" if(y)"bla bla bla"}`

But this does not work. Can anyone help me or tell me if this is possible? Thanks!

Taren Shaw
  • 89
  • 6

1 Answers1

0

Your if-statement is not set up quite correctly. You should use if, with the condition in curly brackets and then else also in curly brackets. Instead you put an ifinside another if. Correcting that and use paste0 to convert your vectors into a string should work. Let me know if that is what you are looking for:

```{r showTrue}
show_text=TRUE
x= "my variable x is true"
y= "my variable y is false"
```

Text true: `r if(show_text){paste0("My text shows: ", x , " here")} else{paste0("Otherwise: my text is " , y, " instead")}`

```{r showFalse}
show_text= FALSE
x= "my variable x is true"
y= "my variable y is false"
```

Text false: `r if(show_text){paste0("My text shows: ", x , "here")} else{paste0("Otherwise: my text is " , y, " here")}`

enter image description here

ViviG
  • 1,613
  • 1
  • 9
  • 23