Compensating R speed with C programming

I have given R for quite a while, even though I appreciate the concept and the function of it so much, my interest was smuggled by numerous times of waiting for my MCMC result! Therefore, one thunder blasted night, I betrayed R and devoted to Matlab.

It is always so painful, since my heart was tortured by the contrite of my betrayal. However, once I decided to dig into C programming for more powerful computation, all my headache is solved!

The limitation of R is due to its continuous quoting of C. Obviously, if we could compute directly with C and output results with R. Should it be fine? Anyway, I am still working on it, hopefully, the conclusion could be appeared shortly!

A short example to demonstrate the integration between R and C programming

First of all, certain packages are required such as compilers for C, which I recommend MinGW, Perl.
Secondly, add R directory into the system path, which is in the properties of "My Computer".
Thirdly, code a C program, which I recommend the classical,
-------------------------------------------------------------------
#include
void hello(void)
{
Rprintf("Hello world\n");
}
-------------------------------------------------------------------
certain modification should be noticed "#include" is needed, and the input of the function should be void,
"print" is replaced by "Rprintf" to print text on R console.

Now let's start. The following steps are programed under dos environment (the command window)
C:\temp\Rcmd SHLIB hello.c *to create a .dll file to connect C program to R
C:\temp\R *to invoke Rterm which ensures R package under dos environment.
followings are command in R console
>>dyn.load("hello.dll")
>>.C("hello")
Hello world
list()
>>

JOB DONE!

标签:,


标签: ,

一条评论 发表在“Compensating R speed with C programming”上

  1. IMHO, for R, the best reference is the inline docs,
    for C type, it is fun if u can use it to build a real system like R, including all the interesting functions!

留下回复