---
title: How to Show R Inline Code Blocks in R Markdown
author: T. Hovorka
date: '2017-12-04'
slug: how-to-show-r-inline-code-blocks-in-r-markdown
categories: 
 - R Language
 - Applications
tags: 
 - R Language
 - R Markdown
---


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

## Inline code with R Markdown
 [R Markdown](http://rmarkdown.rstudio.com/) is a well-known tool for reproducible science in R. In this article, I will focus on a few tricks with R [inline code](http://rmarkdown.rstudio.com/lesson-4.html).
 
Some time ago, I was writing a vignette for my package [WordR](https://CRAN.R-project.org/package=WordR). I was using [R Markdown](http://rmarkdown.rstudio.com/). At one point I wanted to show `` `r "\u0060r expression\u0060"` `` in the output, exactly as it is shown here, as an inline [code block](http://rmarkdown.rstudio.com/authoring_pandoc_markdown.html#verbatim_(code)_blocks). 

In both R Markdown and Markdown, we can write <span style="color:blue">`` `abc` ``</span> to show `abc`.
 What is not obvious is that you can use double backticks to escape single backticks in the code block. So code like this: <span style="color:blue">``` `` `abc` `` ```</span> (mind the spaces!) produces this `` `abc` ``.

Now as an exercise, you can guess how I produced the ``` `` `abc` `` ``` block above. Yes, indeed, I have <span style="color:blue">```` ``` `` `abc` `` ``` ````</span> in the Rmd source file. And we can go on like this ad infinitum (can we?).
 
OK, but I wanted to produce `` `r "\u0060r expression\u0060"` ``. Learning the lesson above, we can try <span style="color:blue">`r "\u0060\u0060\u0060 \u0060\u0060 \u0060r expression\u0060 \u0060\u0060 \u0060\u0060\u0060" `</span>. But trying this, I was getting an error:

```
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: <Anonymous> ... paste -> hook -> .inline.hook -> format_sci -> vapply
Execution halted
```
Obviously, the R Markdown renderer is trying to evaluate the `expression`. So it seems that R Markdown renderer does not know that it should (should it?) skip R inline code blocks which are enclosed by double backticks.  

##Solution

Making a long (and yes, I spent some time to find a solution) story short. The correct code block to produce `` `r "\u0060r expression\u0060"` `` is <span style="color:blue">``` `r "\u0060\u0060 \u0060r \"\\u0060r expression\\u0060\"\u0060 \u0060\u0060"` ```</span>.

Short explanation how it works: `\\u0060` is an Unicode representation of the backtick (`` ` ``). So first, the R Markdown renderer finds the R expression within the double backticks and it evaluates it. Important here is the usage of the Unicode for backtick, since using backtick within the expression would result in an error. (We are lucky, that the R Markdown renderer is not running recursively, finding again the R code block and evaluating it again.) So once the R Markdown is done, the Markdown is just seeing `r "\u0060\u0060\u0060 \u0060\u0060 \u0060r expression\u0060 \u0060\u0060 \u0060\u0060\u0060" ` in the temporary `.md` file, and it evaluates it correctly to `` `r "\u0060r expression\u0060"` `` in the HTML output.

If you want to see (much) more, just look at the source R Markdown file for this article [here](/post/2017-12-01-how-to-show-r-inline-code-blocks-in-r-markdown.Rmd).
Do you know a better, more elegant solution? If you do, please use the discussion below.

---

##Epilogue
Some time after I sent the draft of this blog to the [RViews](https://rviews.rstudio.com/) admin, I got a reply (thank you!) which pointed to the [knitr FAQ page](https://yihui.name/knitr/faq/), specifically question number 7 (and a [new post](https://yihui.name/en/2017/11/knitr-verbatim-code-chunk/) from author of [knitr](https://CRAN.R-project.org/package=knitr) package explaining it a little further). It suggests probably more elegant solution of using

<pre style="color:blue">
Some text before inline code `` `r
expression` `` and some code after
</pre>


(mind the newline!) that will produce ``Some text before inline code `r
expression` and some text after`` or use 
<span style="color:blue">``` `` `r
knitr::inline_expr("expression")` `` ```</span> which produces similarly `` `r knitr::inline_expr("expression")` ``. 

But, I believe this post (especially its [source](/post/2017-12-01-how-to-show-r-inline-code-blocks-in-r-markdown.Rmd)) might still help someone to understand how the R inline code is evaluated.

