Inspired by @Equation's links, I have found a somewhat kludgy solution. Here's an example:
---
title: "Sample excludes"
author: "Dick Startz"
date: "4/4/2022"
output: pdf_document
params:
cutout: TRUE
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# First header
```{asis, echo = !params$cutout}
## A second header
```
```{r, include = !params$cutout,echo = FALSE}
pi
```
### A third header
```{asis, echo = !params$cutout}
#### A fourth header
```
The elements here are:
(1) Declare a parameter in the YAML header. (Parameters can also be set in a knit call.)
(2) To exclude R Markdown, use an asis
block. Note that the asis
blocks also end with three `, I'm just having trouble getting them to show here.
(3) To exclude a coded chunk, just put the include
option as usual.
This isn't perfect, since it is annoying to have to do markdown and code chunks separately rather than just conditionally block out a whole section. Will probably work for me, but I would be glad to hear any more clever ideas.