# Special Mathematica Cell to work with R code

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

REvaluate["{
data(iris)
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:

  data(iris)
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.

-
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

### The R code highlighter

There is an undocumented symbol

RLinkPrivatercell


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 RLinkPrivatercell will be gone, replaced with the new R code cell.

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

rc := RLinkPrivatercell


and then evaluate rc instead.

Here is a sample screenshot:

### 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:

ClearAll[RLinkPrivatereplR]
RLinkPrivatereplR[code_String] :=
REvaluate["{" <> code <> "}"]


### Limitations

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.

-
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):

Cell[StyleData["R"],
CellMargins->{{66, 10}, {5, 10}},
Evaluatable->True,
Background->RGBColor[1,1,0.85],
AutoQuoteCharacters->{},
CellEvaluationFunction -> (REvaluate["{"<>ToString@#<>"}"]&),
CellFrameLabels -> {{None,"R"},{None,None}},
LanguageCategory->"R",
FormatType->InputForm,
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.)
@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 –  rm -rf Dec 20 '12 at 1:24
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