How to Install/Locate R.H and Rmath.H Header Files

Locating/downloading header files R.h and Rmath for C interfacing with R

They should be in the include folder below R's RHOME folder.

$ R RHOME
/home/rowlings/Downloads/R-3.2.1

$ ls -l `R RHOME`/include
total 108
-rw-r--r-- 1 rowlings rowlings 511 Jun 29 17:34 Rconfig.h
-rw-r--r-- 1 rowlings rowlings 5916 Jun 29 17:34 Rdefines.h
-rw-r--r-- 1 rowlings rowlings 2085 Jun 29 17:34 Rembedded.h
drwxr-xr-x 2 rowlings rowlings 4096 Jun 29 17:34 R_ext
-rw-r--r-- 1 rowlings rowlings 2063 Jun 29 17:34 R.h
-rw-r--r-- 1 rowlings rowlings 4818 Jun 29 17:34 Rinterface.h
-rw-r--r-- 1 rowlings rowlings 46137 Jun 29 17:34 Rinternals.h
-rw-r--r-- 1 rowlings rowlings 17619 Jun 29 17:34 Rmath.h
-rw-r--r-- 1 rowlings rowlings 509 Jun 29 17:34 Rversion.h
-rw-r--r-- 1 rowlings rowlings 2142 Jun 29 17:34 S.h

R.h and Rmath.h in native C program

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 $

Compiling C code with R header files

You also want to do

sudo apt-get install r-mathlib

which contains, among other things, the file /usr/include/Rmath.h.

R throwing errors when trying to compile

You have to change

#include <R.h>
#include <Rinternals.h>

to

#include "R.h"
#include "Rinternals.h"

if the header files are in the same directory like your code files.
Or you could add the correct include-path to gcc commandline (e.g. -I/usr/share/R/include).



Related Topics



Leave a reply



Submit