Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

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

1 Answer 1

up vote 10 down vote accepted

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)) )
  }"]

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 at 16:22
    
@pjc42 Do you mean to generalize the above to wrap around an arbitrary piece of R code? –  Leonid Shifrin Apr 17 at 19:27

Your Answer

 
discard

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.