Installing R on OS X

NOTE: The comments are a must read for this. Some excellent additional advice and “gotchas” by some super-helpful readers.


I was in a conversation with an academic colleague (wicked smart dude) and the subject of installing R came up (NOTE: this will happen to you, too, if you ever have the misfortune to have a face-to-face convo with me ;-). They noted that getting up and running with R was not as seamless as one would like it to be and, to be honest, I have to agree, especially after typing the rest of this post out.

I recently had a similar experience helping folks who use Windows get R & RStudio up and running and that’s even more of a nightmare, especially if you do not have Administrator privileges (or, perhaps I just scare easily).

Prior to these experiences, I never really stopped to consider just how less friendly the installation process of R is when compared to Excel, Tableau or other apps one might use for data analysis and visualization. Hopefully this will becomre a top priority for the R Consortium.

Since this colleague uses OS X, I offered to put together instructions for how to get R & RStudio installed and finally had 5 minutes to crank out a blog post to help the broader community with the information.


Get R

R_for_Mac_OS_X


Verify R itself is working

  • Look in the Applications folder for the R application.
  • Double-click it and you should see an R console window.
  • If that did not work, try installing R again
  • Once you’ve verified R is working, quit the app

R_Console


Download RStudio

RStudio is an integrated development environment for R that will make your life and coding easier.

RStudio_-_Download_RStudio


Verify RStudio & R are working together

  • Look in the Applications folder for the RStudio application.
  • Double-click it and you should see an RStudio window with four panes.

niasra_uow_edu_au_content_groups_public__web__inf__math_documents_mm_uow183084_pdf

From now on, just start RStudio when you want to work in R.


[Optional] Install XQuartz

Some functions in R require an “X11 Server” and/or libraries associated with an X11 server. Apple does not provide this software with OS X anymore so you have to do it on your own via a third-party application called XQuartz.

XQuartz


[Optional] Install Xcode Tools

Some R packages require compilation. That requires utilities not installed on OS X by default. You can wait to do the following until it’s needed, but since you’re already installing things…

  • Get Xcode https://itunes.apple.com/us/app/xcode/id497799835?mt=12 and install it like any “normal” Mac application
  • When the intallation is done, open Xcode then close it just to verify it installed correctly
  • Find and open the Terminal program in the Utilities folder under the Applications folder
  • Paste the following into the Termainal window and hit enter/return (accept any dialog/prompt):
xcode-select --install`
  • Close the Terminal application

[Optional] Set yourself up for easier future compiled package installation

Some R packages need additional libraries to work and most aren’t on your system by default. There are a myriad of ways to get these libraries, and the way I obtain them is via the homebrew utility. You can save yourself the trouble of installing homebrew later by doing the following now:

  • Find and open the Terminal program in the Utilities folder under the Applications folder
  • Paste the following into the Terminal window and hit enter/return:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  • Read and accept the various prompts until it’s installed
  • Close the Terminal application

You can now do brew install xyz in the future when a library is needed to support a package. Drop a note in the comments if you’d like this discussed more in a future blog post.


[Optional] [If you have an hour+ to kill] Install MacTeX

R has an academic history and there are many semi-advanced functions that are tied to something called latex. Installing latex for OS X is not hard, just time (and bandwidth) consuming (it’s about the same size as a new OS X installer). If you delve into package creation or do more detailed output work in R, you’ll want to install MacTex sooner than later.


Fin

If you have any changes/additions/etc drop a note in the comments. I may even stick this on github to make it easier to contribute in the future.

Data-Driven Security Podcast
Episode 29: With Great Power Law…
R World News Podcast — Episode 2
Buy on AmazonDDS Blog
DDS PodcastAmazon Author Page

11 Comments Installing R on OS X

  1. Spencer Boucher

    Much easier with homebrew, and also easier to keep up to date:

    “`

    R

    brew tap homebrew/science brew install R

    Rstudio

    brew tap caskroom/cask brew install rstudio “`

    Reply
    1. hrbrmstr

      Totally agree, but the main reason I didn’t include the bit for “R” proper is that anyone seeking “official” help on the R mailing lists (and sometimes even on SO) will get berated by the “old guaRd”. I think I do need to turn this into a mini-wiki or open source ebook since keeping up to date with RStudio (dailies) is so easy this way.

      Reply
    1. hrbrmstr

      ARGH. Yes! I totally forgot to overtly state that and I’ve been bit before on new installs on OS X when I forget to ensure it’s available (my linux installs are scripted and include this). I’ll update the post later today. Huge #ty for the reminder.

      Reply
      1. clarkrichards

        Great post! I’ve been thinking of typing something like this up, even for my own benefit, as the little bits of install notes I leave lying around never seem to make a complete picture when I try and help someone else get their machine to be able to build packages as mine can.

        Lately I’ve found the gfortran step one of the hardest. In particular, the package that is (kindly) provided at:

        https://cran.r-project.org/bin/macosx/tools/

        will not work for the “Mavericks” version of R, which requires gfortran-4.8.2.

        There is a “mystery” page that I’ve found (but don’t know if anything specifically links to it) that provides a binary for gfortran-4.8:

        http://r.research.att.com/libs/

        However, there seems to be some inconsistency based on particular architectures. For “older” machines, that binary is too new, and won’t work in Mavericks-R. For some machines (late early 2009 Macbook Aluminum) I found that installing gfortran-4.8.2 from:

        https://gcc.gnu.org/wiki/GFortranBinaries#MacOS

        worked, but for another (late 2008 Macbook aluminum) it didn’t work. In that case, I ended up having to go with homebrew R, since it was the only way to get the R binaries and gfortran compilers consistent.

        Reply
  2. Simon Garnier

    For command line and package manager addicts like me, there is also the possibility to install R via homebrew or macports. It makes keeping R up to date easier IMO.

    Reply
    1. hrbrmstr

      Totally agree, but the main reason I didn’t include that is that anyone seeking “official” help on the R mailing lists (and sometimes even on SO) will get berated by the “old guaRd”. I think I do need to turn this into a mini-wiki or open source ebook.

      Reply
      1. Brian O'Meara

        The R community is now used to online books like Hadley Wickham’s excellent ones, so you could set up something like that on github. Though I imagine people will start finding this page, regardless.

        Reply
  3. Pingback: Installing R on OS X | rud.is

  4. Pingback: Installing R on OS X – “100% Homebrew Edition” | rud.is

  5. Ben Miller

    Just to clarify about XCode installation, you wrote: * When the intallation is done, open Xcode then close it just to verify it installed correctly * Find and open the Terminal program in the Utilities folder under the Applications folder * Paste the following into the Termainal window and hit enter/return (accept any dialog/prompt): xcode-select –install`

    1) It looks like the command in Terminal is supposed to perform the installation; yes? Or, if we’ve already double-clicked the icon in the Applications folder and used the GUI installer, is there an additional step? 2) If there’s no additional step, maybe the first bullet I quoted above was supposed to say “download” instead of “installation”? It did take quite a while for the program to be available to click on. 3) The Terminal command seems to include a single trailing backtick, which seems like a typo. Is it an extra character, or is there another option that was supposed to be enclosed in backticks but got cut off?

    Reply

Leave a Reply