This directory contains simple examples of using the .C and .Call interfaces for calling compiled C code from R. Usually you would use the package mechanism to hide some of these details, but sometimes it is useful to use this approach. .C Interface ============ The file addoneDotC.c contains a function to add one to each element of a vector. Create a dynamically loadable library (DLL), also called a shared object or a shared library, at the shell command line using R CMD SHLIB addoneDotC.c This compiles the code and links it into a DLL. On Linux this DLL is called addoneDotC.so; other operating systems use other extensions From within R evaluate the expression dyn.load("addoneDotC.so") to load the shared library. You will need to use the appropriate name on other operating systems; one way to make the loadign expression portable is to write dyn.load(paste("addoneDotC", .Platform$dynlib.ext, sep = "")) Now load the R function addoneDotC defined in the file addoneDotC.R with source("addoneDotC.R") and try it out with something like addoneDotC(1:3) .Call Interface =============== The .Call interface is a little more involved but also more flexible. The file addoneDotCall.c creates a .Call interface to the function defined for the .C interface. Compile it to a shared library with R CMD SHLIB addoneDotCall.c load the shared library into R with dyn.load("addoneDotCall.so") and try it out with something like .Call("addoneDotCall", as.double(1:3)) It would be best to again wrap this in an R function. Compiler Flags ============== You can provide additional flags to the C compiler with the PKG_CFLAGS environment variagle. For example, one way to ensure C code is compiled with -Wall -pedantic is to use env PKG_CFLAGS="-Wall -pedantic" R CMD SHLIB addoneDotC.c You can also set this variable in the file ~/.R/Makevars. This file would contain the line PKG_CFLAGS=-Wall -pedantic You will need to create the directory ~/.R (i.e. the directory .R in your home directory) if it does not exist.