繁体   English   中英

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

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

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

我目前的解决方法很难看:

---
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用户,在数学命令中需要双斜线也很烦人。)

还有另一种方法吗?

您可以像在rmarkdown一样编写解决方案(例如,通常使用text, latex和R代码块的组合),而不是使用cat从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) {"-->"}`

这不要求我编辑任何这样包含的条件执行代码块或其他任何类似的代码块。 希望这有助于某人!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM