tha*_*sic 18 markdown r pandoc knitr r-markdown
我是一名教师,希望通过更改我创建的文档参数,从同一个Rmarkdown文件中完成作业和家庭作业解决方案指南soln.当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.
")
```
Run Code Online (Sandbox Code Playgroud)
我想要做的是用cat编写解决方案指南的人更换优雅和可读的内容来替换包含函数的块.我目前的方法对我来说足够了,但我不能让我的合作教师使用它,因为在cat函数内部编写解决方案真是太不愉快了.(作为一名LaTeX用户,在数学命令中需要双斜线也很烦人.)
还有另一种方法吗?
eip*_*i10 24
cat您可以像rmarkdown往常一样编写解决方案(例如,通常使用文本latex和R代码块的组合),而不是使用从R代码块中打印解决方案,并使用参数soln注释掉该部分当您不想在最终文档中包含解决方案时.
在rmarkdown下面的示例文档中,如果参数soln是FALSE,则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}"}`
Run Code Online (Sandbox Code Playgroud)
此外,您可以通过render在单独的R脚本中运行该函数来渲染这两个版本,而不是以交互方式编写上述文件.例如,假设调用上面的文件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"))
}
Run Code Online (Sandbox Code Playgroud)
下面是Solutions.doc看起来像.Homework.doc是类似的,除了大胆的单词之外的所有内容都Solution:被排除在外:
| 归档时间: |
|
| 查看次数: |
4815 次 |
| 最近记录: |