My R setup with Mac OS X

[This article was first published on Milk Trader, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 their blog: Milk Trader.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)