Search code examples
rcompilationcompiler-optimizationjit

Options when compiling R code


I usually just copy-paste (or source(..)) my R code on a console, so that compilation in R is very obscure to me.

When compiling C++ code, one can give indication to the compiler (such as #define or #pragma for example) or specific flag to indicate level of optimization (such as -o2 for example).

Is there an equivalent of #InfoForCompiler or -o2 in R? Is it possible to make specific choices about how and when (when defining the function or when calling the function for the first time) an R code is compiled?


Solution

  • Whichever way you choose to compile, from R CMD COMPILE to R CMD INSTALL for a package, or via something like Rcpp::sourceCpp(), it will always be invoked by R itself and

    • R respect environment variables like CXXFLAGS
    • R looks up its values in $RHOME/etc/Makeconf (on Debian systems also in /etc/R/Makeconf
    • These can be overriden via ~/.R/Makeconf on a per-user basis.

    But as @nrussell commented this will not affect your already-compiled R interpreter, only incremental new compilations -- for example package installations.

    To change this for R itself, you have to essentially rebuild R from source.