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")
```
source
share