
I am trying to write a dynamic linked library for R, in Pascal. (This  
is to speed up the execution of a simulation that I am running in R.)  
I know Pascal might not be the perfect language for this (C or Fortran  
being more natural), but from what I have read I think it should  
work.  Though I should point out that I am a neophyte when it comes to  
DLLs.

 From R I want to hand a function in the library a two-dimensional  
matrix, operate on the matrix, and hand back a 2-d matrix.

I have no problem creating the library and loading it using dyn.load()

I have written an R wrapper for the function.

When I do something simple using scalars (integers) everything works  
fine, or so it seems at least.
However, when I try to use a vector as an argument to the function,  
two things happen:
R becomes very unstable;
the function only operates on the first 3 elements of the vector (say  
if the vector has 5 elements).

Anyone have experience with this sort of thing?

Thanks,
Robin Cowan

Sample below:

This one works fine:

library Test1Lib;

type
pA=^integer;

procedure simple(x:pA); cdecl;
var i1,i2:integer;

begin
	x^:=x^*2;
end;

exports  simple;
begin
end.


This one does not:

library Test2Lib;

type
array1=array[1..5] of integer;
pA=^array1;
procedure simpleArray(x:pA); cdecl;
var i1:integer;

begin
for i1:=1 to 5 do
	x^[i1]:=x^[i1]*2;
end;

exports  simpleArray;
begin
end.


Here is the wrapper I use:
MySimple <- function(x)
{
   ans <- .C("simple",as.integer(x)) # or simpleArray in the second case
   ans[[1]]
   }

Here is what I get:

 > x<-c(1,2,3,4,5)
 > MySimple(x)
[1] 2 4 6 4 5




The University of Maastricht has changed its name and mail servers. My  
email address is now r.cowan@maastrichtuniversity.nl.






	[[alternative HTML version deleted]]

