How to write R code in my posts?

I am a new member of this group. How can I write the R code while commenting or posting? I read also the previously asked questions related to this but don't get my answer.

When you are writing a question or an answer, a set of icons is visible above the text field.

One icon is a pair of curly braces, $\{\}$. To add a block of code, highlight the block and click the curly braces.

This is a block of code


You will see that the curly braces are just adding four spaces at the beginning of each line. When you have a block of code within a list you will need 8 spaces (four to keep the list indentation intact and four more for the code highlighting), which you will have to add manually. Deeper indentations can be handled similarly.

1. This begins my list of stuff.

Here is a block of code nested within the list


This is a continuation of the first list item, indicated by indenting it four spaces.

1. This is a second-level list item, created by a four-space indentation.

2. Another second-level item

A block of code, indicated by indenting 12 spaces.

3. Another second-level item

2. This is recognized as the second list item because it was preceded by a numeral and not indented.

If you want to mark a small part within a sentence as code, surround it with backticks (). For example, this is formatted as code within this sentence.

If you need to add backticks within your code, they will be fine within blocks of code (as seen above), but to add them inline, use the escape character \ immediately before each.

Syntax highlighting

If the question is tagged with , then the code will be syntax highlighted (although not very well...). When the question does not have this tag and you want the code highlighting, then preface each code block with the magic line

<!-- language: lang-R -->

You will have to add this HTML comment before every block of R code you want highlighted. On the other hand, if you will have more than one block of code, and you want R syntax highlighting for all of them, use <!-- language-all: lang-r --> instead (see here for more information).

This should not be indented. If all works correctly, this line will be invisible and the code will be highlighted (perhaps after a slight delay).

Other languages are treated similarly: replace "R" by the name of the language.

Interactions with MathJax

The MathJax delimiter for $\TeX$ markup is the dollar sign \$. Unfortunately, MathJax attempts to process text within code blocks. When it encounters one dollar sign it looks for the second--and creates indentation problems in the code: x <- list(a=1, b=2) y <- x$a
z <- x$b # This line is *not* indented originally: MathJax is doing it!  One work-around is to append a little \$-containing comment after each offending line, as in

x <- list(a=1, b=2)
y <- x$a #$ This dollar sign terminates the MathJax processing.
z <- x\$b # This line is now indented correctly.
`

The other work-around is to de-indent the offending line by 4 spaces.

• Boy, my answer sure is informative now... – gung - Reinstate Monica Feb 13 '15 at 18:15
• I've often wondered what was causing that indentation issue!! If bounties were available on Meta, I'd give a +50 for that. – Silverfish Feb 13 '15 at 19:05
• @Silverfish, FTR I have considerably more rep than I need. At any rate, that part of 'my' answer was written by whuber, who is also the person who figured this out originally (see here). – gung - Reinstate Monica Feb 13 '15 at 23:04
• Well, many thanks to @whuber then. I've quite often spent time re-re-rewritting my R code until finally, magically, the indentation returned to normal. Now I understand what's going on, a considerable frustration will be eased! – Silverfish Feb 13 '15 at 23:11
• Thanx @gung it realy work.How can I do the same in comments? as I asked in the second part of my question. – sumaira sarwar Feb 16 '15 at 4:54
• @sumairasarwar, you use the backticks as I discussed; you cannot have code blocks in comments. – gung - Reinstate Monica Feb 16 '15 at 14:33
• ok thanx @gung. – sumaira sarwar Feb 17 '15 at 7:29
• @sumairasarwar, you're welcome. If this has resolved your question, you may want to accept it by clicking the check mark to its left. – gung - Reinstate Monica Feb 17 '15 at 14:07