Thursday, June 13, 2013

Getting set up to call C from R (and Rstudio) on my Mac

I've recently begun to consider the possibility of using C to speed up some of my R code. This is a big step.

 Now, don't get me wrong. I'm not turning into one of the people who complain about how horrendously slow and clunky R is because for the most part, I think they're probably just not using R well. I am the queen of the {l,s,t, }apply. I have oil paintings of the phrase "Avoid loops at all cost" all about my abode. But, sometimes, even if you are an apply-ninja, try as you might, you just can't avoid a loop. (Damn you, MCMC!)

So, I embarked upon the journey to set up my computer to compile C in such a way that I can call that  code in R. It seems like most tutorials assume that you already use C and compilers and whatnot. So, for those of us who are using this as a gateway to more hardcore programming rather than vice versa (i.e. this is your first time venturing into the world of compilers,  command line prompts, etc.), (1) congratulations; and (2) the following are a detailed set of steps for setting up your computer so that C and Rstudio can play nice together, all communicated without the typical condescension you are met with in R forums response to basic questions (to which I say ...  ).

First, here are some relevant details about my computer:

MacBook Pro

Processor  2.4 GHz Intel Core i7 
Memory  8 GB 1333 MHz DDR3
Software  OS X 10.8 (12A269)

Rstudio (type version into Rstudio to see this)
platform       x86_64-apple-darwin9.8.0     
arch           x86_64                       
os             darwin9.8.0                  
system         x86_64, darwin9.8.0          
status                                      
major          2                            
minor          15.2                         
year           2012                         
month          10                           
day            26                           
svn rev        61015                        
language       R                            
version.string R version 2.15.2 (2012-10-26) (Yeah, yeah, I'll update...)
nickname       Trick or Treat 

Now that that's out of the way, on to the main event!


 Install XCode AND (and this important)  Install gcc/LLVM compiler.  (follow this link)

If you do not complete the second step, you will probably be getting warnings like  the following(even if you are using the inline package):

    /Library/Frameworks/R.framework/Resources/include/R.h:32:18: error: math.h: No such file or directory

    /Library/Frameworks/R.framework/Resources/include/R.h:29:20: error: stdlib.h: No such file or directory

    /Library/Frameworks/R.framework/Resources/include/R.h:30:73: error: stdio.h: No such file or directory

At this point, if you have some C code ready to go, you could compile it and run it in R (not R64).
Rstudio uses the x86_64 architecture, so if you try to load your compiled  .so file using Rstudio, you will probably get the something like:

    no suitable image found.  Did find: /Users/you/your_compiled_file.so: mach-o, but wrong architecture

I found the solution to this problem here. In short, go to your terminal and paste in the following:

 vi /Library/Frameworks/R.framework/Resources/bin/R  

You should see a file that begins with
    #!/bin/sh
    # Shell wrapper for R executable.

    R_HOME_DIR=/Library/Frameworks/R.framework/Resources
Type ":46" (no quotation marks) and press enter. This will take you to the 46th line, where you will see something like
    # Since this script can be called recursively, we allow R_ARCH to
    # be overridden from the environment.
    # This script is shared by parallel installs, so nothing in it should
    # depend on the sub-architecture except the default here.
    : ${R_ARCH = `arch`}
Press "i" to insert and change  this to
    # Since this script can be called recursively, we allow R_ARCH to
    # be overridden from the environment.
    # This script is shared by parallel installs, so nothing in it should
    # depend on the sub-architecture except the default here.
    : ${R_ARCH = /x86_64}
Save and quit by typing in "esc" then ":x" then enter.

So, now you're basically ready to go! I'll let the experts take it from here... you can follow any of the many tutorials that are around. For example, this one.  
Try adding #include &lt R.h &gt or #include &lt stdio.h &gt to the top of your .c file (foo.c in the above tutorial) to make sure everything's up and running!
Hopefully you're now all set up to write some awesome code for R!