Why Would a Mathematica User Care about R?
May 22, 2013 — Jon McLoone , Director, Technical Communication & Strategy
The benefits of linking from Mathematica to other languages and tools differ from case to case. But unusually, in the case of the new RLink in Mathematica 9, I think the benefits have very little to do with R, the language. The real benefit, I believe, is in the connection it makes to the R community.
When we first added the MathLink libraries for C, there were real benefits in farming out intensive numerical work (though Mathematica performance improvements over the years and development of the compiler have greatly reduced the occasions where that would be worth the effort). Creating an Excel link added an alternative interface paradigm to Mathematica that wasn’t available in the Mathematica front end. But in the case of R, it isn’t immediately obvious that it does many things that you can’t already do in Mathematica or many that it does significantly better.
However, with RLink I now have immediate access to the work of the R community through the add-on libraries that they have created to extend R into their field. A great zoo of these free libraries fill out thousands of niches–sometimes popular, sometimes obscure–but lots of them. There are over 4,000 right here and more elsewhere. At a stroke, all of them are made immediately available to the Mathematica environment, interpreted through the R language runtime.
Let’s look at a simple example. While Mathematica supports FisherRatioTest, it doesn’t know the exact Fisher test. (This is a hypothesis test where the null hypothesis is that rows and columns in a contingency table with fixed marginals are independent.)
Well, now it does.
Finding the right library is more work than phoning Tank, and I skipped over any error checking. But the only complicated bit was extracting the p-value from the result (the “[[1,1,1]]” part) because RFunction returns an RObject that contains additional metadata, which, this time, I didn’t care about.
I can now use this just like any built-in function.
I can plot it:
I can manipulate it:
And I can use it with libraries from other languages in a similar way:
The future is always hard to predict. When I started here (many) years ago, general linking to FORTRAN seemed like the most important thing, but no one ever asks me about that any more–C and Java linking are the most popular. Links to some specific libraries (BLAS/LAPACK, GMP, and others) have ended up being core infrastructure components in Mathematica. Whether RLink finds extensive use in Mathematica‘s future features, or remains a more or less stand-alone added chunk of functionality within Mathematica‘s infrastructure, is not yet clear.
There are also issues that need to be considered. R code isn’t going to handle symbolic arguments or high-precision numbers, so, for robustness, you will want to type-check more carefully than you might with Mathematica code. You don’t always have the elegant design and quality of Mathematica. Some of it is quite raw, while some of it is excellent. But design and quality take time and resources, and so it will be quite a while before we fill out Mathematica to fill every one of these niches, and through RLink they are available right now.
A big chunk of extra functionality just became a part of the Mathematica ecosystem.