Search code examples
rcheaderrcpp

What is the `<Rinternals.h>` header, and when is it installed?


I am starting to look at using .Call to run C code within R
(I am doing this before I start to use Rcpp).

I saw that we need to include the header file Rinternals.h at the start of the C program with:

#include <Rinternals.h>

I was wondering what this header file is exactly, and when is it installed? In other words, is this header file part of the C compiler or is it installed with R.


Solution

  • You can ask R about that:

    > file.path( R.home("include"), "Rinternals.h" )
    [1] "/usr/share/R/include/Rinternals.h"
    > 
    

    (This reflects that on my system /usr/lib/ and /usr/share/ both have an R directory, Linux distros may differ here. They key point is R knows from compile- and build time, and provides the info. Beats hard-coding /usr/lib as some Makefile version in the wild.)

    (In general, there is a ridiculous wealth of tools in base R packages utils and tools. They are also well written!)

    Moreover, you don't even need to know! If you test .Call() by doing

    R CMD COMPILE myfile.cpp
    R CMD SHLIB myfile.o
    

    then R will know where to find the headers and library files from. You are still stuck with having to explicitly load the shared library file whose extensions name will differ by operating system. All this is ... indeed much easier with Rcpp and its helpers sourceCpp() or cppFunction().