R/C Code

From QERM Wiki
(Difference between revisions)
Jump to: navigation, search
m (Minor corrections)
m (Simple example)
Line 16: Line 16:
 
To load what you just built to use in R:
 
To load what you just built to use in R:
 
  dyn.load( file.path(path, paste("foo", .Platform$dynlib.ext, sep="")) )
 
  dyn.load( file.path(path, paste("foo", .Platform$dynlib.ext, sep="")) )
 +
 +
Note: if you're on the mac and having issues with 32-bit vs. 64-bit (an error that says "mach-o, but wrong architecture"), you may need to specify the architecture: <code>--arch=i386</code> for 32-bit and <code>--arch=x86_64</code> for 64-bit, depending on which version of R.app you're running.

Revision as of 23:27, 14 December 2010

Contents

Overview

Sometimes R users need to encounter C, for example, to maintain an R package, or to speed up existing functions. It is fairly straightforward to run C code from within R. Pdf10.png Here is how, with some easy-to-follow examples. More detailed information can be found in Writing R Extensions.

Tools needed

Mac/Linux

Everything should be already installed. You may need to install developer tools on Mac if they're not already there.

Windows

Install Rtools to get the necessary tools to compile code and build packages for R. Be careful if you already have Cygwin installed.

Simple example

From the command line:

R CMD SHLIB foo.c 

(you may need the whole path to R if it's not in your path)


To load what you just built to use in R:

dyn.load( file.path(path, paste("foo", .Platform$dynlib.ext, sep="")) )

Note: if you're on the mac and having issues with 32-bit vs. 64-bit (an error that says "mach-o, but wrong architecture"), you may need to specify the architecture: --arch=i386 for 32-bit and --arch=x86_64 for 64-bit, depending on which version of R.app you're running.

Personal tools