Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
493 views
in Technique[技术] by (71.8m points)

markdown - How to display verbatim inline r code with backticks using Rmarkdown?

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

> cat('x60', '
')
` 

Then you write

`r 'x60r foo+barx60'`

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+barx60'` ``

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

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

Also see this post for another trick.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...