34
$\begingroup$

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.

$\endgroup$
4
  • $\begingroup$ 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. $\endgroup$
    – jVincent
    Commented Dec 20, 2012 at 1:03
  • $\begingroup$ 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.) $\endgroup$
    – Joel Klein
    Commented Dec 20, 2012 at 2:11
  • $\begingroup$ 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. $\endgroup$ Commented Dec 20, 2012 at 4:55
  • $\begingroup$ @m_goldberg tks for the editing. Much better! $\endgroup$
    – Murta
    Commented Dec 20, 2012 at 10:10

2 Answers 2

26
$\begingroup$

The R code highlighter

There is an undocumented symbol

RLink`Private`rcell

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:

ClearAll[RLink`Private`replR]
RLink`Private`replR[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.

$\endgroup$
11
  • $\begingroup$ Your code is now part of v9? :D $\endgroup$
    – Mr.Wizard
    Commented Dec 20, 2012 at 11:47
  • 6
    $\begingroup$ @Mr.Wizard Yep:). I developed RLink. But the highlighter part is experimental and not documented therefore. $\endgroup$ Commented Dec 20, 2012 at 11:47
  • $\begingroup$ 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. $\endgroup$
    – Mr.Wizard
    Commented Dec 20, 2012 at 11:50
  • $\begingroup$ @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. $\endgroup$ Commented Dec 20, 2012 at 11:54
  • $\begingroup$ Well if I did my memory decline has entered free-fall, so let's assume I didn't. :-s $\endgroup$
    – Mr.Wizard
    Commented Dec 20, 2012 at 11:57
30
$\begingroup$

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",
    MenuCommandKey->"8"
]

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

$\endgroup$
5
  • $\begingroup$ Very nice!.. but how I implement it as a new style? $\endgroup$
    – Murta
    Commented Dec 20, 2012 at 1:22
  • $\begingroup$ @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 $\endgroup$
    – rm -rf
    Commented Dec 20, 2012 at 1:24
  • $\begingroup$ Cool!.. It worked like a charm. Tks! $\endgroup$
    – Murta
    Commented Dec 20, 2012 at 1:31
  • 1
    $\begingroup$ 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). $\endgroup$ Commented Dec 20, 2012 at 5:30
  • $\begingroup$ 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) $\endgroup$
    – rm -rf
    Commented Dec 20, 2012 at 7:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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