Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

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 use Mathematica with R because I am able to keep my code and output organized with the notebook interface. Although I can get R structures into Mathematica using REvaluate, I can't figure out how to get textual output, from say:


Perhaps this can be done by redirecting output to a temporary file, like the case of plots, but I wonder if there is a native solution.

share|improve this question
I'm getting close with REvaluate["capture.output(summary(airquality))"] // TableForm – Eric Brown Dec 1 '12 at 2:39
Well, that's exactly what I also suggested, without seeing your comment. Took me a while already while answering since SE image upload is somehow broken for me - I was trying (unsuccessfully) to insert the image of the output for a good 20 minutes. – Leonid Shifrin Dec 1 '12 at 2:56
Well, it finally worked. – Leonid Shifrin Dec 1 '12 at 3:01
up vote 11 down vote accepted

Here is a possibility:


and now

StringJoin@Riffle[#, "\n"] &@
     reg <- lm( Sepal.Length ~ Species, data=iris )
     summary.text <- capture.output(print( summary(reg)) )

enter image description here

share|improve this answer
Indeed it was a duplicate. Sorry about that. Perhaps it would be useful to have an REvaluate-like function which produces text by default. Here's why: it seems that in some cases it's best to just evaluate a number of R inputs without transferring anything at all to Mathematica. When I have something that I want to use in Mma, only then I can transfer it explicitly. Another difficulty I had was that it seems difficult to prevent REvaluate from returning any output at all and translating complex and large output (which I don't need anyway) can take a long time. – Szabolcs Apr 18 '13 at 16:32
@Szabolcs Re: suppressing transfer - place a semicolon at the end of the string R code, and that will do it (it is covered in the docs). Re: function - indeed, agree. What I was thinking of doing was to implement R object references (so far using closure-based emulation) as a full-fledged R data type, using the type-extension mechanism. Then, I could use some meta-programming to write a Mathematica-based "RSummary" function, so that, when it is wrapped around REvaluate, we automatically get the summary rather than the imported object. This is quite possible and actually not hard. – Leonid Shifrin Apr 18 '13 at 16:39
@Szabolcs Re: transfer suppression - see this tutorial, section Executing R Code and Getting Data from R. – Leonid Shifrin Apr 18 '13 at 16:43
@LeonidShifrin This is incredibly helpful, thank you. Do you have a bare bones RSummary that we could start working with? – pjc42 Apr 17 '14 at 16:22
@pjc42 Re: RLink - thanks, it's good to know that it is useful. Re: summary - you can then simply do something like RSummary[rcode_String] :=StringJoin@Riffle[#, "\n"] &@REvaluate["capture.output(print( summary(" <> rcode <> ")) )"]. You can call it like RSummary["reg"] (from the above example), where in place of "reg" you can use a variable or just a piece of code which evaluates to some R expression, for which you want the summary. – Leonid Shifrin Apr 18 '14 at 17:56

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.