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 ?
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.
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` ``")
```
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