有条件地在R Markdown中显示文本块 [英] Conditionally display a block of text in R Markdown

查看:575
本文介绍了有条件地在R Markdown中显示文本块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用knitr解析R Markdown文档.有没有一种方法可以根据我传递给knitr的环境中的变量在R Markdown中有条件地显示文本块?

I am using knitr to parse an R Markdown document . Is there a way to conditionally display a block of text in R Markdown depending on a variable in the environment I pass into knitr?

例如,类似:

`r if(show.text) {`
  la la la
`r }`

如果show.text为true,则将在结果文档中打印"la la la".

Would print "la la la" in the resulting doc if show.text is true.

推荐答案

您需要一个完整的R表达式,因此您不能像显示的那样将其分成多个块,但是如果块的结果是文本字符串,则它将被原样包含(不带引号),因此您应该可以执行以下操作:

You need a complete R expression, so you cannot break it into multiple blocks like you show, but if the results of a block are a text string then it will be included as is (without quotes), so you should be able to do something like:

`r if(show.text){"la la la"}`

,并且仅当show.textTRUE时,它才会包含文本.

and it will include the text if and only if show.text is TRUE.

这篇关于有条件地在R Markdown中显示文本块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆