(This article was first published on

Tasks**B.I.S. dato**, and kindly contributed to R-bloggers)- Wrap for netMotifs
- Submit Abstract for PhD Retreat
- Read Architecture paper
- Özgür's WST data and Uli's RTCA data

- When one wants to embed a C routine in his/her R function/package for the first time, it is defnitely exciting to know how does it work at all.
- Some complex data structures are not yet fully supported by SWIG. In these cases it is also necessary to understand how to write a wrapper manually.

- Write a function in C, for example a toy example of function 'myC'.
void myC(int* lower, int* upper) {

int i;

printf("\t Number \t\t Sqaure Root of the Number\n");

for(i = *lower; i<= *upper; ++i)

printf("\t %d \t\t\t %2.2f \n",i, sqrt((double) i));

} - Create shared objects of the function myC with
`R CMD SHLIB myC.c`

in command line. If neccessary also provide needed parameters, for example -lm. This step produces a myC.so file, namely the shared object. - Use
`dyn.load("myC.so")`

function in a R session to load the shared object. - After loading, the function can be called with
`.C`

function, with the function name in string as the first parameter followed by parameters to be passed. Please keep in mind that the type of parameter must be checked (coerced) correct before passing, otherwise one may face unexpected results. use the toy function, a call to the function is demonstrated by:

res <- .C("myC", lower=as.integer(0), upper=as.integer(8)) - Now you should see the output of the function, as follows:
Number Sqaure Root of the Number

0 0.00

1 1.00

2 1.41

3 1.73

4 2.00

5 2.24

6 2.45

7 2.65

8 2.83 - (Optionally) One could unload the shared library by
`dyn.unload("myC.so")`

.

Back to the topic, following the procedures described above, one can relative easily wrap a C (or other languages, like Fortran or C++) function in R. The wrapping is desired since at least it may help the check/coerce of the types.

Next step is to embed one or more C files in a package, which normally requires the use of

`configure`

and `Makevar`

files. There is also some words spent on this in the WRE tutorial, but to me they are not structured and I did not 'know-how' even after several times of reading. So next step I will also write a demo to show how to do this.To

**leave a comment**for the author, please follow the link and comment on his blog:**B.I.S. dato**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...