<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/how-to-show-r-inline-code-blocks-in-r-markdown/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rviews.rstudio.com/2017/12/04/how-to-show-r-inline-code-blocks-in-r-markdown/"> R Views</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<h2>Inline code with R Markdown</h2>
<p><a rel="nofollow" href="http://rmarkdown.rstudio.com/" target="_blank">R Markdown</a> is a well-known tool for reproducible science in R. In this article, I will focus on a few tricks with R <a rel="nofollow" href="http://rmarkdown.rstudio.com/lesson-4.html" target="_blank">inline code</a>.</p>
<p>Some time ago, I was writing a vignette for my package <a rel="nofollow" href="https://cran.r-project.org/package=WordR" target="_blank">WordR</a>. I was using <a rel="nofollow" href="http://rmarkdown.rstudio.com/" target="_blank">R Markdown</a>. At one point I wanted to show <code>`r expression`</code> in the output, exactly as it is shown here, as an inline <a rel="nofollow" href="http://rmarkdown.rstudio.com/authoring_pandoc_markdown.html#verbatim_(code)_blocks" target="_blank">code block</a>.</p>
<p>In both R Markdown and Markdown, we can write <span><code>`abc`</code></span> to show <code>abc</code> (I will use the blue colour for code blocks showing code as it appears in Rmd file, whereas the default colour will be showing the rendered output). What is not obvious is that you can use double backticks to escape single backticks in the code block. So code like this: <span><code>`` `abc` ``</code></span> (mind the spaces!) produces this <code>`abc`</code>.</p>
<p>Now as an exercise, you can guess how I produced the <code>`` `abc` ``</code> block above. Yes, indeed, I have <span><code>``` `` `abc` `` ```</code></span> in the Rmd source file. And we can go on like this ad infinitum (can we?).</p>
<p>OK, but I wanted to produce <code>`r expression`</code>. Learning the lesson above, we can try <span><code>`` `r expression` ``</code></span>. But trying this, I was getting an error:</p>
<pre><code>processing file: codeBlocks.Rmd
Quitting from lines 12-22 (codeBlocks.Rmd) 
Error in vapply(x, format_sci_one, character(1L), ..., USE.NAMES = FALSE) : 
  values must be length 1,
 but FUN(X[[1]]) result is length 0
Calls: </code></pre></div>