Interfacing C in R

Read all the example. It will later be divided in 3 different options (I suggest to take option 3, which compiles the .dll file directly with R).

 

(Note: Of course, I did not invent this procedure. These notes are a collection of small bits of information I obtained from my PhD and from several websites on the Internet. I sorted them all in an easy to follow way for us statisticians. I am always trying to cite the original on-line source. It is understood that the codes and procedures here listed are used under your own responsibility.)

 

Having said that, let us work with an old and general example from S-Plus manuals.

  • Create the "ar.c" code containing the following lines. You can do it on the Notepad or with Tinn-R or any editor (I suggest Tinn-R).

void ar_c_function (   double *x,   long  *n,   double *phi)

{

long i ;

  for ( i = 1 ; i < *n ; i++ ) x[i] = *phi * x[i-1] + x[i] ;

}

  • Create also the "ar.def" file containing

; ar.def Export Definition File

LIBRARY ar.dll

EXPORTS ar_c_function

  • Create the "ar.dll" shared dinamyc library file with this options: To do this there are 3 possible ways (I suggest the 3rd one, because do not depend on Microsoft interface changes. If you still don't want to use option 3 use option 2 -Microsoft Visual C++ 2008 Express Edition-):
  1. For Visual C++ Microsoft Developer Studio (Version 6) and under Windows XP (and/or previous versions).
  2. For Microsoft Visual C++ 2008 Express Edition (Free-downloadable at microsoft website) and under Windows Vista (and also works for Windows XP). With its corresponding update-services packages.
  3. For compiling the C code with the R terminal in a MS-DOS command prompt window and with Windows XP.
  • You can then load the "ar.dll" file in R or S-PLUS 2000 in the usual way. Of course, change the path of this location for the corresponding where you want to keep your .dll files. For example, with R:

dyn.load("I:\\R\\DLLs\\ar.dll","ar_c_function","cdecl")

ar.compiled <- function(x, phi)

{

.C("ar_c_function",as.double(x),length(x),as.double(phi))[[1]]

}

  • Compare the results between compiled function and non compiled function in R:

Ar <- function (x, phi)

 {

   n <- length(x)

   if (n>1)

   for (i in 2:n) x[i] <- phi * x[i - 1] + x[i]

   x

 }

ar.compiled(1:20, .75)

Ar(1:20,.75)

  • The End. To work easily you can have a list of compiled C codes in the same .def file, however I prefer to do it one by one. Also, without ambiguity you can use always the same name but with diferent file extension. It will work and you won't have the need to come up with new file names. Thus, you are always using the same name for every step and the compiler will not get lost, neither you.
  • NOTE: Remember always that C uses vectors from "zero" to length while R uses arrays from 1 to length.
  • Reading suggestions for interfacing C in R (Google them!):
    • "An Introduction to the .C Interface to R" by Roger D. Peng Jan de Leeuw, UCLA Department of Statistics.
    • "R for Windows FAQ" in cran.r-project.org.
    • "Writing R Extensions" available in R help.
  • Reading suggestions to learn C:
    • "Absolute Beginner's Guide to C" by Greg Perry (Very easy and highly ranked by begginers in C).
    • Kernighan, B.W. and Ritchie, D.M. (1988). "The C programming language". Prentice-Hall Intl. (This is the standard reference for C programmers).