Hide entire pages in markdown document r

I have an r-markdown below. I would like to hide page 2 if the parameter "P" is not equal to A.

Thus, the result will be, if the parameter P! = A, then only 3 pages are created.

Is it possible somehow?

---
title: "Untitled"
output: 
  pdf_document:
    toc: yes
params:
  P: A
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

\newpage


## PAGE2

this is text for page 2

this is text for page 2

```{r cars}
summary(cars)
```

\newpage


## PAGE3

this is text for page 3

```{r pressure, echo=FALSE}
plot(pressure)
```

\newpage 

## PAGE4

this is text for page 4
+6
source share
1 answer

You can comment on a section based on the value of P. Here's a slightly hacky way to do this: merge a section with \iffalse/ \fi. Note that the R code inside the section still needs to be valid to work (it will be compiled by knitr, but latex will be ignored).

```{r, echo=FALSE, results='asis'}
if(params$P != "A") 
  cat("\\iffalse")
```

## PAGE2

this is text for page 2

this is text for page 2

```{r cars}
summary(cars)
```

\newpage

```{r, echo=FALSE, results='asis'}
if(params$P != "A") 
  cat("\\fi")
```
0
source

Source: https://habr.com/ru/post/1017367/


All Articles