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.

  • $\begingroup$ I'm getting close with REvaluate["capture.output(summary(airquality))"] // TableForm $\endgroup$ – Eric Brown Dec 1 '12 at 2:39
  • $\begingroup$ 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. $\endgroup$ – Leonid Shifrin Dec 1 '12 at 2:56
  • $\begingroup$ Well, it finally worked. $\endgroup$ – Leonid Shifrin Dec 1 '12 at 3:01

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

  • $\begingroup$ 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. $\endgroup$ – Szabolcs Apr 18 '13 at 16:32
  • $\begingroup$ @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. $\endgroup$ – Leonid Shifrin Apr 18 '13 at 16:39
  • $\begingroup$ @Szabolcs Re: transfer suppression - see this tutorial, section Executing R Code and Getting Data from R. $\endgroup$ – Leonid Shifrin Apr 18 '13 at 16:43
  • $\begingroup$ @LeonidShifrin This is incredibly helpful, thank you. Do you have a bare bones RSummary that we could start working with? $\endgroup$ – pjc42 Apr 17 '14 at 16:22
  • 1
    $\begingroup$ @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. $\endgroup$ – Leonid Shifrin Apr 18 '14 at 17:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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