My R setup with Mac OS X

February 22, 2011
By

(This article was first published on Milk Trader, and kindly contributed to R-bloggers)

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:

    > install.packages("quantmod")

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.

    [Downloads] $

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
    [~]$ cd RScripts
    [RScripts]$ git init
    Initialized empty Git repository in /Users/milktrader/Rscripts/.git/
    [RScripts (master)]$

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 .
    [RScripts (master)]$ git commit -m "more R scripts"
    [RScripts (master)]$ git push
    [RScripts (master)]$ git status

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 command(Apple Key) + r . You'll need to get the R bundle, but that's no big deal. It's a bit easier than compiling R packages from source.  

To leave a comment for the author, please follow the link and comment on his blog: Milk Trader.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , , ,

Comments are closed.