title: Is there a way to have conditional markdown chunk execution in Rmarkdown?
body: <p>I am an instructor looking to make a homework assignment and homework solution guide from the same Rmarkdown file by changing a document parameter I created called <code>soln</code>. When <code>soln=FALSE</code> the assignment document is generated, and when <code>soln=TRUE</code> the homework solution guide is generated. I can control R code chunk execution using the document parameter, but I would also like conditional inclusion of markdown text.</p>
  
  <p>My current workaround is ugly:</p>
  
  <pre><code>---
  title: "Homework"
  output: word_document
  params:
    soln: TRUE
  ---
  Fit the linear regression model $Y \sim X$ with the following data.     
  Interpret the coefficient estimates.
  ```{r promptchunk, include = TRUE, echo = TRUE}
  # R code I want to show in the question prompt goes here
  # This executes in both assignment and solution versions
  set.seed(123)
  X &lt;- c(1, 1, 0, 0)
  Y &lt;- rnorm(4)
  ```
  ```{r, include = params$soln, echo = FALSE, results = "asis"}
  cat("
  **ANSWER**
  ")
  ```
  ```{r, echo = params$soln, include = params$soln, eval = params$soln}
  # R code corresponding to the solution
  fit1 &lt;- lm(Y ~ X)
  summary(fit1)
  ```
  ```{r, include = params$soln, echo = FALSE, eval = params$soln, results = "asis"}
  cat("
  The interpretation of the intercept is.... 
  Our estimate $\\hat{\\beta}_0$ is ",coef(fit1)[1],".
  The estimated X coefficient $\\hat{\\beta}_1$ is ",coef(fit1)[2]," 
  This can be interpreted as....
  
  You can imagine that for more difficult questions, this section could be quite long.
  ")
  ```
  </code></pre>
  
  <p>What I would like to do is to replace the chunks containing <code>cat</code> functions with something more elegant and readable for the person writing the solutions guide. My current approach works enough for me, but it is not something that I could ask my co-instructors to use because it is so unpleasant to write the solutions inside of the <code>cat</code> function. (As a LaTeX user, it is also annoying to need double slashes for everything inside the math commands.)</p>
  
  <p>Is there another way to do this?</p>