簡體   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