有条件地在R Markdown中显示文本块

编程入门 行业动态 更新时间:2024-10-28 02:33:25
本文介绍了有条件地在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.text为TRUE时,它才会包含文本.

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

更多推荐

有条件地在R Markdown中显示文本块

本文发布于:2023-07-26 11:39:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1215994.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有条件   文本   Markdown

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!