0

I want to embed statistics in an rMarkdown/notebook depending on whether an if test is passed.

I haven't found a question that addresses this in SO, but apologies if I've overlooked it.

Based on this link I found how to use an if statement to determine what text goes in, I can simply do:

``` {r}
this_p_value = .03


```
`r if(this_p_value<.05){"this is significant"} else {"this is not significant"}`

If I want to report the p-value I can do:

this_p_value is a significant as p= `r this_p_value`

I've got an answer that shows how you do both, but I imagine there may be a more elegant way than my posted solution (or at least a few alternatives). Apologies again if I've overlooked an SO question addressing this.

  • Did you take a look at [this](https://stackoverflow.com/questions/25407102/conditionally-display-a-block-of-text-in-r-markdown) post? – erocoar Mar 06 '18 at 11:34
  • @ erocoar - that post is how I got the if statement, but it didn't seem to facilitate embedding of an r defined variable into the output. I've edited my question to highlight that question. – beepingbopping Mar 06 '18 at 11:38

2 Answers2

1

Something I've played with, but never fully developed, is a set of functions to make these kinds of constructs a little more manageable in markdown. In this case, 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
}

Which can be used as

---
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`.
Benjamin
  • 16,897
  • 6
  • 45
  • 65
  • Once you've loaded the function, I can imagine this making the manuscript easier to read than my use of paste. – beepingbopping Mar 06 '18 at 12:02
  • I'm sure there's some way to use white space to make it more readable, but I haven't actually used this enough to have developed any experience with it. – Benjamin Mar 06 '18 at 12:03
  • When I tried to run it I needed to install package "checkmate", so edited your code to include this. But it's working nicely already! – beepingbopping Mar 06 '18 at 14:45
  • actually, sorry, I think I'm missing something. When would your function be more optimal than a if([test]){ [output 1] }else{ [output 2] }? I'm not saying it's not, just think I'm not yet understanding why. – beepingbopping Mar 06 '18 at 14:58
  • 1
    It isn't. It's a template to simplify the process of creating the code. And is best suited to including short strings. If you are looking to include longer strings (paragraphs) this probably isn't the right approach. I just find the `toggle_text` to be more descriptive of my goal than a raw `if else` construct. – Benjamin Mar 06 '18 at 15:07
0

So a solution I came up with while writing this question is:

``` {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"}`

But any alternatives to using "paste"?