Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R.h and Rmath.h in native C program

Tags:

c

macos

r

"R.h" and "Rmath.h" are header files for an interface between R.app and C. But, they seems to be readable only through a R command 'R CMD SHLIB something.c'

I wish to compile my native C program to include them using gcc. I'm using Snow Leopard where I'm not able to locate those header files!

Any help?

like image 718
Water in Water Avatar asked Aug 05 '11 18:08

Water in Water


1 Answers

Please see the 'Writing R Extensions' manual about details, you can easily compile and link against Rmath.h and the standalone R Math library -- but not R.h. (Which you can use via Rcpp / RInside but that is a different story.)

There are a number of examples floating around for use of libRmath, one is in the manual itself. Here is one I ship in the Debian package r-mathlib containing this standalone math library:

/* copyright header omitted here for brevity */

#define MATHLIB_STANDALONE 1
#include <Rmath.h>

#include <stdio.h>
typedef enum {
    BUGGY_KINDERMAN_RAMAGE,
    AHRENS_DIETER,
    BOX_MULLER,
    USER_NORM,
    INVERSION,
    KINDERMAN_RAMAGE
} N01type;

int
main(int argc, char** argv)
{
/* something to force the library to be included */
    qnorm(0.7, 0.0, 1.0, 0, 0);
    printf("*** loaded '%s'\n", argv[0]);
    set_seed(123, 456);
    N01_kind = AHRENS_DIETER;
    printf("one normal %f\n", norm_rand());
    set_seed(123, 456);
    N01_kind = BOX_MULLER;
    printf("normal via BM %f\n", norm_rand());

    return 0;
}

and on Linux you simply build like this (as I place the library and header in standard locations in the package; add -I and -L as needed on OS X)

/tmp $ cp -vax /usr/share/doc/r-mathlib/examples/test.c mathlibtest.c
`/usr/share/doc/r-mathlib/examples/test.c' -> `mathlibtest.c'
/tmp $ gcc -o mathlibtest mathlibtest.c -lRmath -lm
/tmp $ ./mathlibtest
*** loaded '/tmp/mathlibtest'
one normal 1.119638
normal via BM -1.734578
/tmp $ 
like image 127
Dirk Eddelbuettel Avatar answered Nov 06 '22 00:11

Dirk Eddelbuettel