compile {callme} | R Documentation |
Compile C code and create wrapper functions to call from R
Description
This function uses the R CMD SHLIB
process to compile
C code into a linked library. This library is then loaded, and
appropriate functions created in R to call into this library. See also: ?SHLIB
Usage
compile(
code,
CFLAGS = NULL,
PKG_CPPFLAGS = NULL,
PKG_LIBS = NULL,
env = parent.frame(),
overwrite = "callme",
verbosity = 0,
invisible = FALSE
)
Arguments
code |
C code following the |
CFLAGS |
character string of flags for the C compiler. e.g. "-O3"
Default: NULL. If specified this value will replace the
default |
PKG_CPPFLAGS |
character string of flags for the C pre-processor.
Flags such as "-I", "-D" and "-U" go here.
Default: NULL
e.g. |
PKG_LIBS |
character string of flags for linking. "-L" and "-l" flags
go here. Default: NULL.
e.g. |
env |
environment into which to assign the R wrapper functions.
Default: |
overwrite |
Which existing variables can be overwritten when wrapper functions are created in the given environment? An error will be raised if the name of the wrapper function already exists in the environment and permission has not been given to overwrite. |
verbosity |
Level of output: Default: 0. Max level: 4 |
invisible |
Should the R wrapper function return the result invisibly?
Default: FALSE. Set this to
|
Value
Invisibly returns a named list of R functions. Each R function
calls to the equivalent C function. If env
is specified,
then these wrapper functions are assigned in the given
environment.
Examples
code <- "
#include <R.h>
#include <Rinternals.h>
// Add 2 numbers
SEXP add(SEXP val1, SEXP val2) {
return ScalarReal(asReal(val1) + asReal(val2));
}
// Multiply 2 numbers
SEXP mul(SEXP val1, SEXP val2) {
return ScalarReal(asReal(val1) * asReal(val2));
}
// sqrt elements in a vector
SEXP new_sqrt(SEXP vec) {
SEXP res = PROTECT(allocVector(REALSXP, length(vec)));
double *res_ptr = REAL(res);
double *vec_ptr = REAL(vec);
for (int i = 0; i < length(vec); i++) {
res_ptr[i] = sqrt(vec_ptr[i]);
}
UNPROTECT(1);
return res;
}
"
# compile the code and load into R
compile(code)
# Call the functions
add(99.5, 0.5)
mul(99.5, 0.5)
new_sqrt(c(1, 10, 100, 1000))