LaTeX Error: Environment comment undefined

By clicking on "Knit To PDF" in RStudio generating report , and using TOC to jump among pages. Assigned variable `selid’ to 1, this program runs successfully because the value 1 exists in data table. If changing ‘selid’ to 2, this program fails because 2 doesn't exist in data table. The error message is "! LaTeX Error: Environment comment undefined." The eval=run_it logic should prevent from no data error, but ... Why is this and how to fix ?

---
title: 
output:
  pdf_document:
    toc: yes
    toc_depth: 3
---

```{r prelim, include=FALSE}
library(dplyr)

data <- data.frame(
  stringsAsFactors = FALSE,
  orgid= c("1","3"),
  orgnm= c("School1","School3")
)
selid <- "1"
```
\pagebreak

## School: `r selid`  
**This is page 1 - Instruction**    
**Purpose: Testing table of content (TOC)** 

Generating pdf report using TOC to jump among pages. It contains summary and detail pages for testing. If let variable **selid** equals to 1, this program runs successfully because value 1 exists in data table (**data**). If changing variable **selid** equals to 2,this program fails because 2 doesn't exist in data table. The error message is "! LaTeX Error: Environment comment undefined." 

```{r echo=FALSE}
check <- data %>% filter(orgid==selid)
if(nrow(check)>0){run_it <- TRUE} else {run_it <- FALSE}
```
```{r echo=FALSE, results='asis', eval=run_it}
cat("\n\n\\pagebreak\n")
``` 
`r if(!run_it) {"\\begin{comment}"}`
## Summary Report  
School ID `r selid`  
**This is page 2 - Summary Report**       
`r if(!run_it) {"\\end{comment}"}`

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.