Discovering Statistics

A den for Learning

My little Note on Integrating C/C++ in R

For quite a few weeks, I have been struggling to integrate C in R. My sole purpose on building up this page is to keep track of everything I am learning regarding integration of C / C++ in R.

First Week(Hello World!!)

I started by writing a simple code in R Studio bearing the name hello.c extension.

void hello(){
   printf("Hello World!!");

I thought that ransacking tutorials in the internet would be enough to bring this function up to R. I had read earlier that a ‘.so’ is to be created and loaded using dyn.load() in R. Being an “Rcpp” package user, I was a bit too over-confident. Hence my nightmare started:

I tried to use the Mac OS TERMINAL and clanged my file. OMG!! R.h file not found was the output I got. I went up to the src files and checked that the file R.h exists. Being unable to generate the ‘.so’ file was bugging me a lot, so I searched further into R documentation and found out that there is a command R CMD SHLIB which can generate an ‘.so’ file for further use. However whenever I tried to use R CMD SHLIB hello.c, in the terminal, I was not able run the program, it was solely because the R.h file was still not found.

Down and broken, I tried the system2(“R”,c(“CMD” “SHLIB”,”hello.c”))) in the R console and got the generated file.


NOTE TO SELF: The program finally works now. DON’T forget to use dyn.unload(“hello”) at the end.