Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have enjoyed working with R inside Mathematica. But it would be nice If I could have a special R cell. For example, instead of typing

      reg <- lm( Sepal.Length ~ Species, data=iris )
      summary.text <- capture.output(print( summary(reg)) )

I would change the cell style to the special R code one and simply type in the code as I would do in a terminal window. Like so:

  reg <- lm( Sepal.Length ~ Species, data=iris )
  summary.text <- capture.output(print( summary(reg)) )

Is there some way to create such a FrontEnd cell?

In the future I would like Mathematica to have code highlighting and autocompletion for such cells. This would make Mathematica the best environment for working with R.

share|improve this question
This should give you one such cell: CellPrint[TextCell["x + b", "Input", CellEvaluationFunction -> (REvaluate[#1] &)]]. I belive you shoud be able to define this on a stylesheet level however, but it's not working for me right now. – jVincent Dec 20 '12 at 1:03
Very nice! Question, can't you just say CellEvaluationFunction->REvaluate? REvaluate should be equivalent to (REvaluate[#1]&) (by eta-expansion), unless CellEvaluationFunction passes more than one argument. (But since the default is Identity which takes just 1 arg, I think this is safe.) – Joel Klein Dec 20 '12 at 2:11
I think it can take a second argument which by default is InputForm -- at least thats what I conclude when I use Print as the function. – Mike Honeychurch Dec 20 '12 at 4:55
@m_goldberg tks for the editing. Much better! – Murta Dec 20 '12 at 10:10
up vote 21 down vote accepted

The R code highlighter

There is an undocumented symbol


When you type it and press Shift+Enter, you get a syntax-highlighted R code cell based on my real-time syntax highlighter, which is connected to REvaluate and highlights the code as you type. Note that the cell where you type RLink`Private`rcell will be gone, replaced with the new R code cell.

If it is too much typing, you can make an alias like so:

rc := RLink`Private`rcell

and then evaluate rc instead.

Here is a sample screenshot:

enter image description here

Some details

As noted in my answer about the highlighter, it does perform bracket and parentheses matching / highlighting as well. To make it more responsive, I disables re-parsing on all keys except left/right arrows and space bar, so you will have to press one of those to re-render. Normally they are pressed most frequently when working with code, so this should not be very noticable. Also, the cursor may disappear at times, but again, pressing these keys should make it re-appear. The same remark goes to the visibility of block selection.

Note that, as present, rcell does not understand the multi-line code string without curly braces. One way to fix this is to execute this code:

RLink`Private`replR[code_String] :=
    REvaluate["{" <> code <> "}"]


For Windows, the highlighter works fine. For other platforms, the highlighter was giving me crashes on some Linux flavors and on Mac. But, I got reports for other Linux flavors that it worked fine there. So, ymmv, but you can give it a try.

Apart from problems on specific platforms, the highlighter may have a few glitches. If you find some, I would appreciate if you report them to me.

share|improve this answer
Your code is now part of v9? :D – Mr.Wizard Dec 20 '12 at 11:47
@Mr.Wizard Yep:). I developed RLink. But the highlighter part is experimental and not documented therefore. – Leonid Shifrin Dec 20 '12 at 11:47
Now I understand why you were so busy for a while! Should I have known this earlier? I seem to do very poorly at staying informed. – Mr.Wizard Dec 20 '12 at 11:50
@Mr.Wizard Yes, I was busy mainly because of it. Re:earlier - I told folks that I consult for WRI and was developing RLink right when V9 was released, so about 3 weeks ago, in chat. Somehow I was sure that you saw that chat transcript. – Leonid Shifrin Dec 20 '12 at 11:54
Well if I did my memory decline has entered free-fall, so let's assume I didn't. :-s – Mr.Wizard Dec 20 '12 at 11:57

The following is a style for an R cell, inspired by this answer. You can add this to your stylesheet (change the styles to your liking):

    CellMargins->{{66, 10}, {5, 10}},
    CellEvaluationFunction -> (REvaluate["{"<>ToString@#<>"}"]&),
    CellFrameLabels -> {{None,"R"},{None,None}},
    FontFamily -> "Inconsolata-dz",

You can now create a new R cell by pressing Cmd8, but you'll have to manually remove the MenuCommandKey setting from the "Code" style before it'll work. Alternately, you can simply press Cmd0 and choose R from the list.

You then use it just like any other input/code cell. Paste your R code into the cell and hit ShiftReturn and it'll evaluate it (assuming RLink has already been called, etc.)

enter image description here

share|improve this answer
Very nice!.. but how I implement it as a new style? – Murta Dec 20 '12 at 1:22
@Murta Go to: Format > Edit Stylesheet > Enter a style name and enter R in the field. In the new style cell, press Cmd-Shift-E and then delete whatever is in there and paste the above. I explain the steps with pictures in the first part of this answer – R. M. Dec 20 '12 at 1:24
Cool!.. It worked like a charm. Tks! – Murta Dec 20 '12 at 1:31
I just tested this and found a problem. ` AutoQuoteCharacters->{}` is required otherwise if you enter a link to download data you get curly quotes which R doesn't like. I've taken the liberty of editing the answer. I have left LanguageCategory->"R" even though I don't think it is valid (but does not seem to cause an error). – Mike Honeychurch Dec 20 '12 at 5:30
Thanks for the edit @Mike :) I know there isn't any language category called "R", but I left it in there, just in case future versions introduce support for R syntax highlighting (and because, as you said, it doesn't cause any issues) – R. M. Dec 20 '12 at 7:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.