I am using an R Notebook to generate a personalized report for multiple people. Each report will be based on a different subset of the same dataset. Within the report, I have a section there I talk about the "most positive number" in a list of numbers, and the "most negative number" in that same list of numbers.
The tricky part is that it's not guaranteed that all numbers in the list will be both positive and negative. In some cases, the numbers in the list will only be positive, and in other cases, the numbers in the list will only be negative. But hopefully most times, the numbers will be both positive and negative.
With the data included below, I want to end up with the following statements in my report:
---
title: "Report"
output: pdf_document
---
```{r echo=FALSE}
# create some sample data
data <- structure(list(
value = c(0.0877467265976158, 0.0470430107526882,
0.0379081350304372, 0.0251588983050848, -0.000220385674931101,
-0.00389321468298109, -0.0079051383399209, -0.0100182149362477,
-0.0173333333333333, -0.0198838937656, -0.0436432637571157, -0.0771637863594339),
value2 = c(0.00769948802859674, 0.00221304486067754, 0.00143702670148586,
0.000632970163925598, 4.85698457148368e-08, 1.51571205677796e-05,
6.24912121732874e-05, 0.000100364630508856, 0.000300444444444444,
0.000395369231281665, 0.00190473447137317, 0.00595424992532435)),
class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -12L))
```
The following code cell SHOULD BE executed:
```{asis echo=max(data$value > 0)}
The most positive number for value is `r max(data$value)`
```
The following code cell SHOULD BE executed:
```{asis echo=min(data$value) < 0}
The most negative number for value is `r min(data$value)`
```
The following code cell SHOULD BE executed:
```{asis echo=max(data$value2 > 0)}
The most positive number for value2 is `r max(data$value2)`
```
The following code cell SHOULD NOT BE executed:
```{asis echo=min(data$value2) < 0}
The most negative number for value2 is `r min(data$value2)`
```
Does this work for you?
---
title: "Report"
output: pdf_document
---
```{r echo=FALSE}
# create some sample data
data <- structure(list(
value = c(0.0877467265976158, 0.0470430107526882,
0.0379081350304372, 0.0251588983050848, -0.000220385674931101,
-0.00389321468298109, -0.0079051383399209, -0.0100182149362477,
-0.0173333333333333, -0.0198838937656, -0.0436432637571157, -0.0771637863594339),
value2 = c(0.00769948802859674, 0.00221304486067754, 0.00143702670148586,
0.000632970163925598, 4.85698457148368e-08, 1.51571205677796e-05,
6.24912121732874e-05, 0.000100364630508856, 0.000300444444444444,
0.000395369231281665, 0.00190473447137317, 0.00595424992532435)),
class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -12L))
text <- c('* The most', 'number for')
```
The following code cell SHOULD BE executed:
```{r, results = 'asis', echo = F, eval=max(data$value > 0)}
cat(text[1],'positive',text[2],'value is',max(data$value), sep=' ')
```
The following code cell SHOULD BE executed:
```{r, results = 'asis', echo = F, eval=min(data$value) < 0}
cat(text[1],'negative',text[2],'value is',min(data$value), sep=' ')
```
The following code cell SHOULD BE executed:
```{r, results = 'asis', echo = F, eval=max(data$value2 > 0)}
cat(text[1],'positive',text[2],'value2 is',max(data$value2), sep=' ')
```
The following code cell SHOULD NOT BE executed:
```{r, results = 'asis',echo = F, eval=min(data$value2) < 0}
cat(text[1],'negative',text[2],'value2 is',min(data$value2), sep=' ')
```