38

By doubling the backticks in Markdown, it is easy to render some text in code style including the backticks, such as: `r 2+2`. But how to do that with RMarkdown ? By the same way we can display `t 2+2`, but replacing t with r executes the R code 2+2.

The only way I have found so far is:

<p><code  class="r">`</code><code class="r">r 2+2`</code></p>

Not very convenient. Maybe I should define a new css for doing that more conveniently ?

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
  • 1
    There's no escape operation (typically done with "\" in R) in RMarkdown? – IRTFM Dec 05 '13 at 19:51
  • @DWin I don't see what you have in mind. – Stéphane Laurent Dec 05 '13 at 20:06
  • @agstudy This is not R HTML (I have never used that by the way). HTML is allowed in RMarkdown. Indeed, I'd like to have a solution without HTML (but I'd like to get the r style in addition). – Stéphane Laurent Dec 05 '13 at 20:36
  • If you want to protect a back-tick from being parsed as a backtick in R, you can just escape it: `strange.name <- 'abc\\\`def'`: `nchar(strange.name) [1] 8` (It even looks like it survived the SO version of Markdown except for the fact that I used _two_ backslashes at the console and only one was showing up in this comment until I added a third one.) – IRTFM Dec 05 '13 at 20:37
  • @DWin I'm under the impression you have never used Rmarkdown. When doing `r 2+2` in Rmarkown between backticks, one gets 4 in the output. I'm afraid your comment has nothing to do with this issue. – Stéphane Laurent Dec 05 '13 at 20:40
  • Your impression is correct, but I do not think your demonstration proves there is no escape sequence in Markdown. That was my question. – IRTFM Dec 05 '13 at 20:41
  • @Dwin Following your suggestion, this works : `r cat(\\`r 2+2 \\`)` (between backticks) Maybe this is what you meant... – Stéphane Laurent Dec 05 '13 at 20:42
  • No sorry that doesn't work... – Stéphane Laurent Dec 05 '13 at 20:44
  • I dont suppose that `\`2+2\`` does what you want? It won't specifically be highlighted for R, but it will be set in monospace font ... oh, no, I see you want the backticks included verbatim ... – Ben Bolker Dec 05 '13 at 22:34
  • @BenBolker Yes. I have updated the title, I hope it is clearer now ("verbatim" is the appropriate word, thank you) – Stéphane Laurent Dec 05 '13 at 23:53

5 Answers5

38

Here is a trick that I use. First, note \x60 is `:

> cat('\x60', '\n')
` 

Then you write

`r '\x60r foo+bar\x60'`

which will give you `r foo+bar` in the markdown output, but that will become r foo+bar in the HTML output, so you need to protect the backticks in markdown, using two (or more) backticks. Then you end up with this hairball:

`` `r '\x60r foo+bar\x60'` ``

Your own solution is good, but I'd just define

rinline <- function(code) {
  sprintf('``` `r %s` ```', code)
}

Also see this post for another trick.

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
5

To anyone looking at this now, you may want to check out the more recent solution here: embed Rmarkdown without knitr evaluation

Essentially you can do:

Some R code inline : `r knitr::inline_expr("2+2")`

I'm guessing that the functionality describe above has been added to knitr directly but it saves us defining the function ourselves.

adunaic
  • 174
  • 1
  • 5
  • 1
    Wow, this is nice! Especially if you had already loaded `knitr` in the document's session, you can skip `knitr::` – Miguel Alvarez Dec 08 '21 at 08:28
  • Nice. I have needed extra single marks around the double quote-marks: knitr::inline_expr(**'**"2+2"**'**), for some reason. – PatrickT Feb 18 '22 at 23:59
5

The solution of Yihui Xie was not displaying the enclosing quotations in the inserted code when rendering a README.md file for a Github repository. In that case I used html code:

<code>&grave;r foo(x)&grave;</code>

Which displays &grave;r foo(x)&grave; inline.

Miguel Alvarez
  • 314
  • 2
  • 11
4

Here is a satisfactory finding. First define the function

rinline <- function(code){
  html <- '<code  class="r">``` `r CODE` ```</code>'
  sub("CODE", code, html)
}

in an invisible chunk. Then you can show `r 2+2` by typing:

Some R code inline : `r rinline("2+2")` - nice 
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
4

I just learnt about the results='asis' option.
So, yet another way; for fun and learning :-)

```{r, results='asis', echo=FALSE}
cat("`` `r 2+2` ``")
```
aquaraga
  • 4,138
  • 23
  • 29
  • This isn't an in-line solution, which is what the question asked for, as it uses the display environment. – adunaic Dec 09 '21 at 09:31