[R] advice on using this C code in R

baptiste auguie ba208 at exeter.ac.uk
Sun Mar 8 13:18:48 CET 2009


Dear list,


I'd like to ask for some advice in creating a wrapper function for  
this C code,

http://www.fourmilab.ch/documents/specrend/
http://www.fourmilab.ch/documents/specrend/specrend.c

I could probably port it in R, but I've been hoping to use compiled  
code for a while and this looks like a good test case.

Unfortunately, the only success I've had so far is with the too simple  
factorial(int *n) function,

http://math.acadiau.ca/ACMMaC/howtos/C_R.html

Here, however, the code has several functions, some of which use  
structures as input variables and return several values.

I've created a shared library with,

> R CMD SHLIB specrend.c


and then ran,

dyn.load('specrend.so')

> .C('spectrum_to_xyz', spec_intens=as.numeric(1),   
> wavelength=as.numeric(1),

> 					x=numeric(1), y=numeric(1), z=numeric(1))


which obviously resulted in a nice crash,

>  *** caught illegal operation ***
> address 0x942776, cause 'illegal opcode'
>
> Traceback:
>  1: .C("spectrum_to_xyz", spec_intens = as.numeric(1), wavelength =  
> as.numeric(1),     x = numeric(1), y = numeric(1), z = numeric(1))
>
> Possible actions:
> 1: abort (with core dump, if enabled)
> 2: normal R exit
> 3: exit R without saving workspace
> 4: exit R saving workspace
> Selection:


Looking at the C code I want to use, could you direct me to a  
beginner's tutorial for similar problems? Specifically:

- how to deal with a function that returns several values (e.g  
xy_to_upvp here)

- how to deal with structures (perhaps i needn't worry about this, I'm  
not sure)

Many thanks,

baptiste



sessionInfo()
R version 2.8.0 (2008-10-20)
i386-apple-darwin8.11.1

locale:
en_GB.UTF-8/en_GB.UTF-8/C/C/en_GB.UTF-8/en_GB.UTF-8


_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag




More information about the R-help mailing list