Inline Markdown



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.

The implication of the “one or more consecutive lines of text” rule is that Markdown supports “hard-wrapped” text paragraphs. This differs significantly from most other text-to-HTML formatters (including Movable Type’s “Convert Line Breaks” option) which translate every line break character in. Use Markdown to share code You can format and share your code on Teams. To add an inline block of code, start and end the text with a back tick (`), which is located next to the 1 on your keyboard. To add a multi-line block of code, preface your text with ``` and then paste it into your compose box. Free, open-source, full-featured Markdown editor. Synchronization is one of the biggest features of StackEdit. It enables you to synchronize any file in your workspace with other files stored in your Google Drive, your Dropbox and your GitHub accounts. This allows you to keep writing on other devices, collaborate with people you share the file with, integrate easily into your workflow. Similarly, because Markdown supports inline HTML, if you use angle brackets as delimiters for HTML tags, Markdown will treat them as such. R Markdown will always display the results of inline code, but not the code apply relevant text formatting to the results As a result, inline output is indistinguishable from the surrounding text.

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.

Markdown

In both R Markdown and Markdown, we can write `abc` 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: `` `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?).

Inline

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:

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 expressionu0060'` ``.

Inline Code Markdown

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.

Markdown Inline Code Language

Epilogue

Github Markdown Inline Code

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

(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.