问题描述
我正在考虑开发一个.rmd文件,该文件可以根据前面的R结果在输出文件(.html,.pdf,...)中动态编写一些叙述性块.下面简单地说就是我想要的工作方式:
I'm thinking about developing a .rmd file that can dynamically write some chunks of narratives in the output file (.html,.pdf,...) based on the preceding R result. To put it simple below is how I want it works:
```{r,echo=F,include=FALSE}
x=1;
```
##if x=1 then output text below
text1
##if x=2 then output text below
text2
.....
推荐答案
上面的答案很好,但很复杂.使用内联代码,如注释中建议的那样.
The above answer is all good and well, but complicated. Use inline code, like suggested in comment.
The mean of x is `r mean(x)` which is very high...
修改:由于这被选为答案.我会详细说明.您可以使用if()或ifelse()或switch().我喜欢切换最多,更快和更干净的代码.但是我不确定如何在开关中使用else语句.
Since this was selected as the answer. I'll elaborate.You can use if() or ifelse() or switch(). I like switch the most, faster and cleaner code. However I am unsure how to use an else statement in the switch.
```{r}
x <- 1
```
x is `r if(x==1){"equal to one"} else {"not equal to one"}` which is great...
x is `r ifelse(x==1, "equal to one", ifelse(x==2, "equal to two", "not equal to one or two"))` which is great...
x is `r switch(x, "1" = "equal to one", "2" = "equal to two")` which is great...
这篇关于Knitr是否可以在每个块中基于R代码动态输出叙述文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!