Notice how line 587 is not part of the gray block and colors the next text line incorrectly. The Rmd file renders just fine with no errors into a PDF using bookdown. It just seems like RStudio is not displaying it correctly.
If I leave out line 587 (````), I get a mismatched tic warning when building the book, and the tics shown on line 586 do not show up in the PDF file.
Is there something I'm doing wrong here? Is there a better way to do this?
You can put a there to show that this is indeed annoying...
In this case, you can put the content of the chunk in another R Markdown file, and then use the embed engine to include it as verbatim content. The markdown source won't be messed up.
```{embed, file = "my-file.Rmd"}
```
This will add the content of the Rmd file as a verbatim content highlighted as Markdown source