我想在rMarkdown / notebook中嵌入统计信息,具体取决于是否通过了if测试。
我没有找到一个在SO中解决这个问题的问题,但如果我忽略它就道歉。
基于this link,我发现如何使用if语句来确定文本的内容,我可以简单地做到:
``` {r}
this_p_value = .03
```
`r if(this_p_value<.05){"this is significant"} else {"this is not significant"}`
如果我想报告我可以做的p值:
this_p_value is a significant as p= `r this_p_value`
我有一个答案显示你是如何做到的,但我想可能有一个比我发布的解决方案更优雅的方式(或者至少有一些替代方案)。如果我忽略了解决这个问题的SO问题,请再次道歉。
我曾经玩过但从未完全开发的东西是一组函数,可以使这些类型的构造在降价时更易于管理。在这种情况下,toggle_text
toggle_text <- function(condition, true, false)
{
coll <- checkmate::makeAssertCollection()
checkmate::assert_logical(x = condition,
len = 1,
add = coll)
checkmate::assert_character(x = true,
len = 1,
add = coll)
checkmate::assert_character(x = false,
len = 1,
add = coll)
checkmate::reportAssertions(coll)
if (condition) true
else false
}
哪个可以用作
---
title: "Untitled"
output: html_document
---
```{r}
install.packages("checkmate") #comment out if installed
library(checkmate)
toggle_text <- function(condition, true, false)
{
coll <- checkmate::makeAssertCollection()
checkmate::assert_logical(x = condition,
len = 1,
add = coll)
checkmate::assert_character(x = true,
len = 1,
add = coll)
checkmate::assert_character(x = false,
len = 1,
add = coll)
checkmate::reportAssertions(coll)
if (condition) true
else false
}
this_p_value = 0.03
```
This is `r toggle_text(this_p_value <= 0.05, "", "not")` significant as p = `r this_p_value`.
```{r}
this_p_value = 0.07
```
This is `r toggle_text(this_p_value <= 0.05, "", "not")` significant as p = `r this_p_value`.
因此,我在撰写此问题时提出的解决方案是:
``` {r}
this_p_value = .03
```
`r if(this_p_value<.05){paste(" this is significant as p=",this_p_value,". [Rest of writeup here]",sep='')} else {"this is not significant"}`
但任何使用“粘贴”的替代方案?