将整个页面隐藏在r markdown文档中

use*_*875 6 r knitr r-markdown

我有下面的降价文件.如果参数"P"不等于A,我想隐藏第2页.

所以结果是如果参数P!= A那么只产生3页.

这有可能吗?

---
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
Run Code Online (Sandbox Code Playgroud)

sco*_*coa 0

您可以根据 P 值注释掉该部分。这是一种有点黑客的方法:用\iffalse/包围该部分\fi。请注意,该部分内的 R 代码仍然需要有效才能工作(它将由 knit 编译,但被 Latex 忽略)。

```{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")
```
Run Code Online (Sandbox Code Playgroud)