Reputation: 137
I am working on a problem, where I have to print results of some calculations and send them in email using R markdown.
There are several if else conditions in the code, (around 100) every time If Else condition is met, I want to print the generated data-frame in R markdown so that it can be part of email that I will send.
The overview of code looks like-
a <- 5
b <- 6
if (a > b) {
## Print Data frame in R Markdown ##
} else if (b < a) {
## Print Data frame in R Markdown ##
}}
## email the whole result Doc generated by R markdown ##
email part I am able to figure out, but I am not able to slice it down in chunks. every time my R markdown script is failed. any clue or path forward please.
Upvotes: 1
Views: 774
Reputation: 8072
One trick I like to use in situations like this is to set the chunk parameters echo
and eval
by the conditional statements. Achieves the same result as J_F, but has broader applications.
---
title: "Untitled"
output: pdf_document
---
```{r}
a <- 5
b <- 6
```
```{r chunk1, echo = a>b, eval = a>b}
knitr::kable(mtcars[,1:2])
```
```{r chunk2, echo = a<b, eval = a<b}
knitr::kable(mtcars[,3:4])
```
## email the whole result Doc generated by R markdown ##
```
Upvotes: 2
Reputation: 10372
I know, I posted a comment, but this solution should work:
---
title: "Untitled"
output: pdf_document
---
```{r}
a <- 5
b <- 6
if (a > b) {
knitr::kable(mtcars[,1:2])
} else if (a < b) {
knitr::kable(mtcars[,3:4])
}
## email the whole result Doc generated by R markdown ##
```
Upvotes: 2