2016-09-17 33 views
8

我是一位教師,希望通過更改我創建的名爲soln的文檔參數來從同一個Rmarkdown文件中完成作業分配和作業解決方案指南。當soln=FALSE生成作業文檔時,並且soln=TRUE生成作業解決方案指南。我可以使用文檔參數控制R代碼塊的執行,但是我還希望有條件地包含減價文本。有沒有辦法在Rmarkdown中有條件降價組塊執行?

我目前的解決方法是醜陋:

--- 
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用戶,數學命令中的所有內容都需要雙斜槓也很煩人。)

是否有另一種方法可以做到這一點?

+0

你是說,你的當前版本的「作品」,但你會喜歡的東西更容易理解和維護?另外,你可以添加一個你會問的真實問題的例子,以及解決方案,這樣我們就可以瞭解R代碼中需要做什麼以及什麼是文本? – eipi10

+0

@ eip10,我更新了我的問題。目前的版本確實有效,但是有條件的塊執行需要被封裝在「cat」函數中,與典型的Rmarkdown相比,它更直觀,更難讀寫。我試圖讓一般的輔導員有可重複性和Rmarkdown,但是我不能讓他們看到這個爛攤子,並期待買入。我_think_我正在尋找一種方法來繞過R作爲處理某些文本塊的引擎。 – thatssobayesic

+0

編織時是否有條件地包含文件?在代碼塊中執行所有文本似乎很奇怪。也許看看書和/或筆記本。 – Elin

回答

10

代替使用cat打印從R碼塊內的溶液的,可以編寫的溶液,作爲你通常會在(即,與文本,latex通常的組合,且R代碼塊),和利用參數soln在您不想將解決方案包含在最終文檔中時將該部分註釋掉。

在下面文件的樣品,如果該參數是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:以後被排除:

enter image description here

+0

我很震驚你可以在輸出到Word的Markdown文件中包含LaTeX頭文件。這非常有幫助。我曾見過類似的方法,但由於不得不使用Word而不是LaTeX,因此將它們寫成不適用於我的情況。這不是一個完美的解決方案,但它遠勝於我所做的。我會再等幾個小時看看是否有其他答案彈出,否則我會接受這個答案。謝謝。 – thatssobayesic

相關問題