The eco-system of R is largely Ubuntu and SVN, so Mac users sometimes find themselves a bit out of place, shall we say. But let’s not bad high-school memories about not being in the in-crowd keep us from participating in the R world. With just a little setup, you can bring your own flair to writing code in R.
Getting R Packages on Your Mac
I’ll leave details of how to install R on a Mac to your own googling efforts, as the process is mostly trivial. But what is not so trivial is compiling packages from source. The best way to load a package is to do it from within the R console with the simple invocation:
This goes searching to the default repository site and looks for your requested package. It does pretty much everything for you. But if you are interested in a package that hasn’t made it to mac binary format (it’s like emptying the dishwasher to some package developers), then you may need to install from source.
The first thing you need to ensure is that you have XCode. This gives you the C compiler. But you’ll also need a Fortran compiler, which can be found here. Once you get that squared up, you’re ready to start downloading cool source code. In the world of R, source code comes with the .tar.gz file extension. Here is a list of source-code package files found at RForge. Download a source file and then switch to that directory in your terminal application.
From there, type the following:
[Downloads]$ R CMD INSTALL xts_0.7-6.17.tar.gz
Now you can hang with the Ubuntu boys.
Creating a Git Repository For Your Scripts
While you have your terminal open, let’s create a repository for all of our R scripts. First, cd back to home.
From there, let’s make a new directory called RScripts. Move over to that directory and initialize it as a git repository.
[~]$ mkdir Rscripts
Hold on, your saying. What is up with this git init?!? Oh, pardon me rushing through things. If you have an account at github, then you won’t need to go here to create an account. Otherwise, please do. Once you have your account, press the Create Repository button and follow the instructions. They are quite simple, really.
The specialized prompt that incorporates the git (master) branch info is a bit of bash wizardry that I copied from someone, but I’m afraid I can’t remember where I got it. Apologies to the author for the lack of attribution. In any case, you don’t really need that. After you create a few scripts, and have done all the steps to create your repository, don’t forget to push frequently and often. It’s a simple mechanical mantra:
[RScripts (master)]$ git add .
Of course, there is a lot of feedback code between all those command line invocations, but we won’t publish that here. It looks neater (and easier to remember) this way.
Running Your Scripts from Textmate
Since we’re on the topic of writing scripts, let’s finish with the topic of text editor. I use TextMate. Let’s say you have a dozen scripts piled up in your RScripts diretory (all properly pushed to github, of course), and you’d like to open one of them to view and run, but cannot remember which one. Easy.
[RScripts (master)]$ mate .
This opens up TextMate with all the files from your directory neatly organized (as well as you organized them in the directory, at least) in a gutter. Here is picture of one of my R script directories within TextMate:
You see how cool that is? To run this script from TextMate, simply press