2

This works in a usual code chunk in R markdown:

m1_aov <- anova(m1)
m1_aov$`Sum Sq`[2] %>% round(3)

Unfortunately, using the latter in inline code breaks the knitr parser down

`r m1_aov$`Sum Sq`[2] %>% round(3)`

Indeed, it also breaks Stackoverflow.

I looked at this related question but could not infer a working solution to my problem. Any hint?

green diod
  • 1,399
  • 3
  • 14
  • 29
  • What is wrong with doing `res <- m1_aov$\`Sum Sq\`[2] %>% round(3)` in a code-chunk and then use \`r res\` inline as suggested by @Frank in the comments of the related question? – Paul May 18 '22 at 06:45
  • Indeed, there's nothing wrong with that. But it would be more streamlined if there was no difference in behavior in both cases. – green diod May 18 '22 at 11:06
  • FYI, contrary to what you’ve said, Stack Overflow has *no* issue with this, unlike ‘knitr’, because Stack Overflow implements CommonMark, which allows using multiple backticks for inline code fences. So you’d write e.g. ```(``r m1_aov$`Sum Sq`[2]``)```. – Konrad Rudolph Aug 22 '22 at 12:34
  • Thank you for your input! I wasn't aware of the multiple backticks trick. – green diod Sep 07 '22 at 14:21

1 Answers1

2

Expanding the comment with a working example:

---
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

```{r}
a <- tibble::tibble(`a column` = 1:10) # using tibble to get a column name with a white space
m <- mean(a$`a column`)
```

Mean is `r m`

To me this looks like a neat trick because it avoids to include unecessary long code inside the text, and do not create the problem you are facing at the (small) cost of creating new objects.

The output: enter image description here

Paul
  • 2,850
  • 1
  • 12
  • 37
  • 2
    It's a good working workaround. But often the inline code is not that long and you also need to think to prepare the extra new objects ahead of the inline code snippet. – green diod May 18 '22 at 11:10
  • "Need to think to prepare ... ahead of the inline code snippet"? That doesn't make sense. R Markdown documents are documents. You can edit them in any order. – user2554330 May 19 '22 at 09:14
  • @user2554330 I think it means that you have to create the object upstream the text actually using it. So the writing process may be less smooth. – Paul May 20 '22 at 06:41
  • @Paul Yes, the process is less smooth and a little bit cumbersome with the need of one-time variables just for output. By the way, is your .png image of the Rmd output a screenshot? – green diod Jun 09 '22 at 17:36
  • @greendiod the .png image is a screenshot of the Rmd output. The Rmd output is a HTML file. Maybe you could suggest this feature on the [github page of knitr](https://github.com/yihui/knitr). Another way could be tu use "standard" names for the column (not a bad idea in general) and avoid the use of ``, *i.e.* replace `Sum Sq` with `Sum_Sq`. – Paul Jun 10 '22 at 06:36
  • @Paul I agree with the use of "simple" names for columns. In this case, the column name is from the output of an (antique) widely used function so users are stuck. – green diod Jun 17 '22 at 06:42