Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

## Inline code with R Markdown

R Markdown is a well-known tool for reproducible science in R. In this article, I will focus on a few tricks with R inline code.

Some time ago, I was writing a vignette for my package WordR. I was using R Markdown. At one point I wanted to show ``r expression`` in the output, exactly as it is shown here, as an inline code block.

In both R Markdown and Markdown, we can write ``abc`` to show `abc` (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: ``` `abc` ``` (mind the spaces!) produces this ``abc``.

Now as an exercise, you can guess how I produced the ``` `abc` ``` block above. Yes, indeed, I have ```` `` `abc` `` ```` in the Rmd source file. And we can go on like this ad infinitum (can we?).

OK, but I wanted to produce ``r expression``. Learning the lesson above, we can try ``` `r expression` ```. 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[]) result is length 0
Calls:  ... 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 expression`` is ``` `r "\u0060r expression\u0060"` ```.

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 expression` ``` in the temporary `.md` file, and it evaluates it correctly to ``r expression`` in the HTML output.

If you want to see (much) more, just look at the source R Markdown file for this article here. 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 admin, I got a reply (thank you!) which pointed to the knitr FAQ page, specifically question number 7 (and a new post from author of knitr package explaining it a little further). It suggests probably more elegant solution of using

```Some text before inline code `` `r
expression` `` and some code after
```

(mind the newline!) that will produce `Some text before inline code `r expression` and some text after` or use ``` `r knitr::inline_expr("expression")` ``` which produces similarly ``r expression``.

But, I believe this post (especially its source) might still help someone to understand how the R inline code is evaluated.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8653868,"math_prob":0.5001588,"size":3687,"snap":"2020-34-2020-40","text_gpt3_token_len":884,"char_repetition_ratio":0.1344013,"word_repetition_ratio":0.051671732,"special_character_ratio":0.24844046,"punctuation_ratio":0.12550607,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.963924,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-08-14T15:25:33Z\",\"WARC-Record-ID\":\"<urn:uuid:392aa46a-504a-4f64-8eb6-9700f2a23a00>\",\"Content-Length\":\"76007\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ce8dc20b-5612-41a1-acbe-28dbd552eb00>\",\"WARC-Concurrent-To\":\"<urn:uuid:2a193032-f33b-4ab2-a5f6-4b8b036b0820>\",\"WARC-IP-Address\":\"104.28.8.205\",\"WARC-Target-URI\":\"https://www.r-bloggers.com/how-to-show-r-inline-code-blocks-in-r-markdown/\",\"WARC-Payload-Digest\":\"sha1:KGLQHVKC27LXZWUEINB3LHQ2TPHFXKQ4\",\"WARC-Block-Digest\":\"sha1:IPOLKCU3X2X6ZKKMBWNQFVYY6VZH4PH4\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-34/CC-MAIN-2020-34_segments_1596439739328.66_warc_CC-MAIN-20200814130401-20200814160401-00433.warc.gz\"}"}