SOME QUICK RECIPES FOR WRITING C FUNCTIONS TO BE CALLED FROM SPLUS OR R. Updated for use with Splus 6.0, and R 1.5.1 -------------------------------------------------------- STEP (1): Write a C program. Here is an example: % cat add.c #include "stdio.h" #include "math.h" #include "stdlib.h" #define PI 3.14159 #define NMAX 100 void add(double *x, double *y, long *nn, double *out) { long n = *nn; int i; for(i=0;i dyn.open("S.so") # can be skipped if you keep the local .Data # directory from Splus CHAPTER add.c R: = > dyn.load("add.so") # This is the default name but it can be changed. ---------------------------------------------------------- STEP (4): Write an S function as follows: [ this is called a "wrapper function"; it just smooths the rough edges between Splus/R and C ] add.fun <- function(x,y){ n <- length(x) out <- as.double(rep(0,n)) z <- .C("add",as.double(x),as.double(y),as.integer(n), out=as.double(out)) z } Note 4: To return something with a name, you must set aside a variable with that name. For example, the variable out is for that purpose. Make sure out is the right length. Note 5: As we will see below, .C returns a list whose elements are all the arguments passed to the C function, modified in whatever way the C function modified them. ---------------------------------------------------------- STEP (5): Try it: x _ c(1,2,3) y _ c(4,5,6) add.fun(x,y) Splus output: ============ $"": [1] 1 2 3 $"": [1] 4 5 6 $"": [1] 3 $out: [1] 5 7 9 R output: ======== [[1]] [1] 1 2 3 [[2]] [1] 4 5 6 [[3]] [1] 3 $out [1] 5 7 9 ---------------------------------------------------------- STEP (6): If you want to modify the source code you have to (a) unlink the old version from Splus or R: Splus: > dyn.close("add.so") R: > dyn.unload("add.so") (b) change the source code (c) recompile and relink using the instructions above ---------------------------------------------------------- MORE HELP BY TYPING Splus: > help(dyn.open) R: > help(dyn.load) AND FOLLOWING THE "RELATED TOPICS" FROM EITHER HELP PAGE. ----------------------------------------------------------