I am trying to only run a specific section of an R markdown file, based on the value of a parameter. For example, in this R markdown file that is adapted from the R markdown typical example (I did not close the r chunks with the '```' symbol in the example as it closes the code area in the StackOverflow question):
---
title: "Untitled"
date: "`r Sys.Date()`"
output: pdf_document
params:
CategReport:
label: "Report with plots?"
value: Choose type of report
input: select
multiple: true
choices: [Plots, Text]
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
if Plots %not in% params$CategReport, don't run the area from below, continue after it ->
{ ### PLOT AREA START
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
### PLOT AREA END}
## Other stuff that will be printed regardless if plot area is skipped or not
What I want is that, if the user chooses the 'Plots' choice in the multiple choice question when knitting with parameters, everything below the 'if Plots %in% params$CategReport, run everything from below ->' will be shown, including the text outside the code chunk.
I've seen some options online to include conditional code chunks based on parameter value, but if I do that I can only include the text in the code chunk. And if I include text in the code chunk, I don't know how to access all the nice R markdown formats like "##" or "**" and the nice text orientation on the page. If I do it in the code chunk, it will appear like code comments.
So I am looking into putting a condition, that, if not met, will skip this entire section of the report, but still show the part of the markdown that is below this part (e.g. '## Other stuff that will be printed regardless if plot area is skipped or not').
Basically, I am looking for something like: if condition met, skip or don't skip this section of the markdown - if not skipped, print it as normal, if skipped, print everything before and after this section (and crucially, this section includes both stuff outside code chunks and stuff inside code chunks).
Is it possible to have something like this?
One option would be to use a child document and pass a condition to the eval=
chunk option so that the child document only gets rendered when the user selects "Plots"
:
---
date: "`r Sys.Date()`"
output: pdf_document
params:
CategReport:
label: "Report with plots?"
value: Choose type of report
input: select
multiple: true
choices: [Plots, Text]
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
```{r eval="Plots" %in% params$CategReport, child = "child.Rmd"}
```
Other stuff that will be printed regardless if plot area is skipped or not
child.Rmd
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```