5

How do I InstallR on OS X to use my already installed and configured R?

Simply using

InstallR["RHomeLocation" -> "/Library/Frameworks/R.framework/Resources"]

results in

$Failed
InstallR::nopaclet: Could not find RLink runtime installed. Please use RLinkResourcesInstall to install it

How do I connect Mathematica to my installed R? Do I have to install a separate R for Mathematica?

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
orome
  • 12,819
  • 3
  • 52
  • 100
  • Note: "/Library/Frameworks/R.framework/Resources" there is what R.home() reports. – orome Mar 10 '14 at 13:42
  • @Szabolcs Sorry, I'm probably missing something. I followed all the instructions. My R version seems ok. But I have the following error: Needs::nocont: "Context "IGraphR` was not created when Needs was evaluated." But IGraphR.m is in the right folder (the one opened by SystemOpen@FileNameJoin[{$UserBaseDirectory, "Applications"}]) – sam84 Apr 08 '14 at 08:20
  • @sam84 If IGraphR.m is really in the right folder, and you get this message, then you don't have the correct IGraphR.m file. Try downloading it again from here – Szabolcs Apr 08 '14 at 13:54
  • @Szabolcs yes, now it works. Thanks! – sam84 Apr 09 '14 at 07:06

1 Answers1

6

Try like this in a fresh kernel:

Needs["RLink`"]

SetEnvironment["DYLD_LIBRARY_PATH" -> "/Library/Frameworks/R.framework/Resources/lib"];

InstallR["RHomeLocation" -> "/Library/Frameworks/R.framework/Resources"];

Update: With Mathematica 10.0.1 (but not 10.0.0), use

InstallR["RHomeLocation" -> "/Library/Frameworks/R.framework/Resources", "RVersion" -> 3]; (* for M 10.0.1 *)

This works if you are using the R distribution from http://www.r-project.org/, but if I recall correctly some people said it didn't work with some other R distributions (e.g. MacPorts's R). I only tried with the official R.

To check that you are indeed running with the chosen version of R, evaluate

REvaluate["R.version.string"]

which on my system returns {"R version 3.0.3 (2014-03-06)"} (as I have R 3.0.3).

I described this before here.


Note: With Mathematica 9.0.1 it is necessary to first make sure that RLink is set up and working with Mathematica's internal R distribution, as described in the user guide. Once that is done, an external R can be used as above. Otherwise InstallR might exit with an InstallR::nopaclet error.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
  • I still get "InstallR::nopaclet: Could not find RLink runtime installed. Please use RLinkResourcesInstall to install it". (I am using the r-project distribution.) – orome Mar 10 '14 at 13:49
  • You must install Mathematica's internal R first. Once that it working, you can use an external R like above. See my update. I assume this will install not only the internal R, but also some other code that's necessary for RLink to work. – Szabolcs Mar 10 '14 at 13:52
  • Ug. MMA is pretty good at messing up things when it is allowed to install. I'm not keen on having two Rs around. Where will it install its R? Can I delete it after installing, or does it need to be around forever? – orome Mar 10 '14 at 13:52
  • @raxacoricofallapatorius It won't mess up anything. It will install it in your $UserBaseDirectory and won't change anything on the system. – Szabolcs Mar 10 '14 at 13:53
  • Excellent. Thanks! – orome Mar 10 '14 at 13:53
  • @raxacoricofallapatorius Actually, if it complains about internal R missing when explicitly passed an "RHomeLocation" option, then this is a bug. I will keep this in mind and look into it when I can. The RLinkResourcesInstall function is concerned strictly with the installation of bundled version of R, and with an external R distro, RLink should be good to go without any additional installation at all. IIRC, this was also so in all my tests, so I am somewhat surprised by this behavior. – Leonid Shifrin Mar 10 '14 at 14:03
  • @LeonidShifrin I always thought there was some other necessary code installed by RLinkResourcesInstall, so I didn't complain about this (I was aware of this behaviour) :) – Szabolcs Mar 10 '14 at 14:04
  • @Szabolcs Actually there isn't, as far as I remember. RLink is Java + Mathematica, and in the case of bundled R distro also a (somewhat non-standard) R distribution plus a few extra dynamic libraries. But in the case of external R, this last part is not needed. I will have to double-check, to be totally sure, it's been a while. – Leonid Shifrin Mar 10 '14 at 14:08
  • @LeonidShifrin: Thanks for looking into this! – orome Mar 10 '14 at 14:25
  • @LeonidShifrin: As long as RLinkResourcesInstall will have no side effects I'm willing to try it now. But if there are any I'll wait to hear from you. From past experience I'm very conservative about letting MMA install anything, especially anything Java-related. – orome Mar 10 '14 at 14:42
  • 1
    @raxacoricofallapatorius RLinkResourcesInstall uses the same paclet mechanism that is used e.g. for downloading the curated built-in data from Wolfram servers. This is not something external, it is part of the general strategy Mathematica uses to download its optional parts, or parts which change in time (such as data). The resulting paclet is installed into a special Paclets subdirectory inside $UserBaseDirectory, where also other packets are installed (mostly, data). The Java part of RLink comes as a part of the core RLink distro, which is a part of Mathematica distribution. – Leonid Shifrin Mar 10 '14 at 14:57
  • @raxacoricofallapatorius So, I don't see any reason to be scared of what RLinkResourcesInstall does, but this is of course up to you. Again, if InstallR with explicit "RHomeLocation" option setting still demands the installation procedure, then it's a bug. – Leonid Shifrin Mar 10 '14 at 15:02
  • 1
    @raxacoricofallapatorius Ok, I looked into the code, and I can see where this bug is coming from. Basically, indeed, the internal R distro is currently required to be installed, even when this is not strictly necessary. So, for now, it is probably easiest to just install the internal version. If you are very much against that, I could write a patch to bypass this and post it here in a comment, but it will be untested (no time now to test it), and things may get wrong in more than one place in such patch. – Leonid Shifrin Mar 10 '14 at 15:30
  • @LeonidShifrin: No need to do a patch but thanks (very much) for the offer; and thanks for following up. If I decide to proceed, I'll report any problems back here; in the meantime I'll just use R directly (and pandas). Thanks again for your help. – orome Mar 10 '14 at 17:06
  • 1
    @raxacoricofallapatorius No problem. Also, I feel sorry that RLink's current state prevents you and some others from using it in the way you'd like to. I do hope to have some dedicated time to improve it some time soon. – Leonid Shifrin Mar 10 '14 at 17:12
  • @LeonidShifrin One of the biggest improvements for me would be a way to make it convenient to always use the external R version. The main obstacle to that now is that it's not possible to just use SetOptions on InstallR to change "RHomeLocation". – Szabolcs Mar 10 '14 at 20:18
  • @Szabolcs You mean, to persist this setting between Mathematica sessions? I'll keep this in mind, I agree that it can be useful. The general approach would be to have savable option configurations / settings - they may also include e.g. R command line arguments, some non-standard JRE location, etc. – Leonid Shifrin Mar 10 '14 at 20:58
  • @LeonidShifrin Persisting it would be excellent, but what I was actually referring to is that currently SetOptions[InstallR, "RHomeLocation" -> "/Library/Frameworks/R.Framework/Resources"]; InstallR[] doesn't actually use that option value, so every time I have to do InstallR["RHomeLocation" -> "/Library/Frameworks/R.Framework/Resources"]. If I could set the option once, then I could have a simple package that will just load RLink, change "DYLD_LIBRARY_PATH" and set this option on InstallR, and always load this package instead of RLink. – Szabolcs Mar 10 '14 at 21:04
  • @Szabolcs This is definitely another bug. This one is easy to fix, but for now you can use option configurators - I have described them here. Or, another possibility is to define Options[installR] = {"RHomeLocation" -> Automatic}; installR[opts___]:= InstallR[opts, Sequence @@ Options[installR]], and then change the options of installR using SetOptions (which is more or less the same thing as what configurators do). – Leonid Shifrin Mar 10 '14 at 21:21
  • If I have to start a new thread, please let me know. Everything works fine with 10.0.0.0, however after Mathematica was updated to 10.0.1.0, the R version that I use cannot be called anymore. InstallR["RHomeLocation" -> "/Library/Frameworks/R.framework/Resources"] Failed to detect the R version from the specified path to R home
    directory. Try using the "RVersion" option to specify the R version explicitly
    – Ed Mendes Sep 28 '14 at 16:32
  • @EdMendes I can't easily answer questions for about 10 more days or so (no reliable internet connection or time to play with Mma), it was a good choice to post a new question on this. – Szabolcs Sep 29 '14 at 15:35
  • @Szabolcs This does not seem to work anymore. I have Mma 10.3.1 and OS X 10.11.2. The InstallR command produces an error message: "Failed to install R. The following error was encountered: Unable to load dynamic libraries". – mef Dec 19 '15 at 12:52
  • @mef OS X 10.11 has the System Integrity Protection feature turned on by default, which prevents changing the DYLD_LIBRARY_PATH variable. See here. – Szabolcs Dec 19 '15 at 18:40
  • @Szabolcs Thank you. And (regarding the fix) hmm... – mef Dec 19 '15 at 18:55
  • 1
    @Szabolcs Thank you. Using the command R.Home() to retrieve the home directory and following your procedure to install RLink successfully. Thank you. – madeinQuant Apr 07 '16 at 08:10