# Getting R textual output into Mathematica

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:

summary(fit)


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.

-
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

Here is a possibility:

Needs["RLink"]
InstallR[]


and now

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


-
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