有没有办法在 Rmarkdown 中执行有条件的降价块?

Is there a way to have conditional markdown chunk execution in Rmarkdown?

我是一名教师,希望通过更改我创建的名为 soln 的文档参数,从同一个 Rmarkdown 文件中制定家庭作业和家庭作业解决方案指南。 soln=FALSE时生成作业文档,soln=TRUE时生成作业解答指南。我可以使用文档参数控制 R 代码块的执行,但我也希望有条件地包含降价文本。

我目前的解决方法很糟糕:

---
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 <- c(1, 1, 0, 0)
Y <- 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 <- 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.
")
```

我想做的是将包含 cat 函数的块替换为对编写解决方案指南的人来说更优雅、更易读的内容。我目前的方法对我来说已经足够了,但我不能要求我的合作教师使用它,因为在 cat 函数中编写解决方案非常不愉快。 (作为 LaTeX 用户,数学命令中的所有内容都需要双斜线也很烦人。)

还有其他方法吗?

不是使用 cat 从 R 代码块中打印解决方案,您可以像通常在 rmarkdown 中那样编写解决方案(即,使用通常的文本组合,latex,和 R 代码块),并在您不想在最终文档中包含解决方案时使用参数 soln 注释掉该部分。

在下面的示例rmarkdown文档中,如果参数solnFALSE,则r if(!params$soln) {"\begin{comment}"}行插入\begin{comment}注释掉解决方案(在最后插入匹配代码 \end{comment})。我还用两个制表符缩进了所有内容,因此问题编号的格式为悬挂缩进。 (如果您喜欢这种格式,则不必为每个新段落或块键入双制表符。如果您对一行执行此操作,则随后每次按 Enter 键时,新行将自动使用双制表符进行格式化。或者,只需键入给定问题的所有文本和代码,然后在完成后突出显示所有内容并键入 tab 两次。)

---
title: "Homework"
output: word_document
header-includes:
  - \usepackage{comment}
params:
  soln: TRUE
---

1. Fit the linear regression model $Y \sim X$ with the following data. Interpret the coefficient estimates.

    ```{r promptchunk, echo = TRUE}
    set.seed(123)
    X <- c(1, 1, 0, 0)
    Y <- rnorm(4)
    ```

`r if(!params$soln) {"\begin{comment}"}`

    **Solution:**

    Run the following R code to fit the linear regression model:
    ```{r, include = params$soln, echo = TRUE, results = "asis"}
    fit1 = lm(Y ~ X)
    ```

    To see a summary of the regression results, run the following code and review the output: 

    ```{r, include = params$soln, echo=TRUE}
    summary(fit1)
    ```
    The interpretation of the intercept is.... 

    Our estimate $\hat{\beta}_0$ is `r round(coef(fit1)[1], 2)`.

    The estimated X coefficient $\hat{\beta}_1$ is `r round(coef(fit1)[2], 2)`. 

    This can be interpreted as....

`r if(!params$soln) {"\end{comment}"}`

此外,您可以通过 运行 在单独的 R 脚本中使用 render 函数来呈现两个版本,而不是交互式地编织上面的文件。例如,假设上面的文件名为 hw.Rmd,打开一个单独的 R 脚本文件并 运行 以下内容:

for (i in c(TRUE, FALSE)) {
  rmarkdown::render("hw.Rmd", 
                    params = list(soln = i),
                    output_file=ifelse(i, "Solutions.doc", "Homework.doc"))
}

下面是 Solutions.doc 的样子。 Homework.doc 类似,只是排除了从粗体字 Solution: 开始的所有内容:

我能够构建 来制作不使用乳胶包的东西(尽管我正在生成 HTML 幻灯片,所以这可能就是它起作用的原因)。

在您希望开始注释的地方,只需添加: `r if(params$soln) {"<!--"}`

然后添加这个以结束评论: `r if(params$soln) {"-->"}`

这不需要我编辑任何因此包含的代码块以进行条件执行或其他类似操作。希望这对某人有所帮助!