# Why Would a *Mathematica* User Care about R?

May 22, 2013 — Jon McLoone, International Business & Strategic Development

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.

Download this post as a Computable Document Format (CDF) file.

## 12 Comments

All very well and good to say that the RLink user has access to all those R libraries. But that’s true, isn’t it only if the user’s platform is Windows? For Mac OS X, say, one can only use the canned version of R plus some selected libraries rather than a native R installation along with whatever R libraries the user may wish to add.

R is fully supported on OSX. http://cran.r-project.org/bin/macosx/

You bring up a very important point. Fortunately it turned out to be possible to use an external R installation on OS X after all: I briefly described how to do it here (see under “Usage examples”):

http://mathematica.stackexchange.com/a/24545/12

Rather than saying that it makes a connection to the R community, it’s probably better to say that it opens the gate to several libraries which have R interfaces. This is why it’s absolutely essential to be able to use an external R installation, not just the one that is auto-downloaded by Mathematica. Murray has a very good point.

Here’s an example where I found it quite useful in practice: https://github.com/szhorvat/IGraphR

You can use a stand-alone R distribution on both Windows and Linux.

However you are correct that this limitation exists for users of OS X. Improvements to R Link, such as this, are under consideration for the next release.

Your opening paragraph begs the question: What prevents the MMA community from being more like the R community?

(call me if you’d like some ideas…)

— Anticipating any reply might mention these —-

Demonstrations Project — Sure, it’s a nice place to show off programming chops, but it does little to foster any sense of community. Why isn’t there a way to rate demonstrations? (so it’s easier to find great demonstrations) Why isn’t it possible to fav demonstrations (and be able to manage those favorites—and perhaps be notified of updates to them—via Wolfram Portal)? Why not make it easy to contact demonstrations authors? (suggest extensions, improvements, bug fixes, pitch an idea, or just to heap praise—-all this could be easily incorporated into Wolfram Portal, no?)

WRI Library — Needs help. Tons of valuable information, but for the most part it’s a mess of seemingly abandoned projects and outdated marketing materials. When the Wolfram Portal was announced, I was positive it would have a way to interact with the Library…

These are all great points, and I know that most of them have been discussed here. I haven’t been part of the ‘community’ team, so I can’t comment on when we might expect these features, but there is a plan, the first step of which is in beta now at http://wolfram.com/community

Despite having access to MM9 at work, I continue to use R because

no software can beat the wealth of libs available for R. I wouldn’t recommend shackling R into mathematica environment With R studio, R coding , lib install and update has become so convinient that it would be an overkill to use MM for accessing R libs

It is unlikely that one would want to use Mathematica PURELY for the purpose of accessing R libraries. However, there are plenty of reasons to use Mathematica which R cannot serve, and being able to integrate R libraries into a Mathematica workflow will, in many cases, be better than keeping them separate.

“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…”

I have to ask, what about FORTRAN?

OK one person has asked me!

One approach is to use C as a wrapper eg

http://stackoverflow.com/questions/8185979/how-to-use-fortran-subroutines-or-functions-in-mathematica

Which references

http://compotato.blogspot.co.uk/2011/09/calling-fortran-code-with-mathlink.html

You can also use .NET link, see an example at

https://groups.google.com/forum/?fromgroups#!topic/comp.soft-sys.math.mathematica/yxS7kD6CmRI

The new LibraryLink functionality

http://reference.wolfram.com/mathematica/guide/LibraryLink.html

should provide another (more performance efficient) route.

Your opening paragraph begs the question: What prevents the MMA community from being more like the R community?