我正在使用knitr来解析R Markdown文档.是否有办法根据我传入knitr的环境中的变量有条件地显示R Markdown中的文本块?
例如,类似于:
`r if(show.text) {`
la la la
`r }`
Run Code Online (Sandbox Code Playgroud)
如果show.text是真的,将在生成的文档中打印"la la la" .
我是一名教师,希望通过更改我创建的文档参数,从同一个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 = …Run Code Online (Sandbox Code Playgroud) 我有一个标题,后跟一个Rmd文件中的代码块.如果满足条件,我只想包含此标题和后跟它的块.我知道如何使用块,因为它在代码的主体中,但我如何做前者?
```{r}
print_option <- TRUE
```
## My header
```{r}
if(print_option==TRUE) {
print (x)
}
```
Run Code Online (Sandbox Code Playgroud)