This is an example of C code calling an R function, by building a function call and then evaluating it.
Created
February 15, 2020 02:50
-
-
Save wch/4c54c765d16fdd9328e763db22512c4f to your computer and use it in GitHub Desktop.
Calling R function from C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(inline) | |
checkver <- cfunction( | |
body = ' | |
SEXP e, ret; | |
bool result; | |
// This is the expression `packageVersion("later") >= "0.8.0.9003"` | |
e = PROTECT( | |
Rf_lang3( | |
Rf_install(">="), | |
Rf_lang2(Rf_install("packageVersion"), Rf_mkString("later")), | |
Rf_mkString("0.8.0.9003") | |
) | |
); | |
ret = Rf_eval(e, R_GlobalEnv); | |
if (TYPEOF(ret) == LGLSXP && LOGICAL(ret)[0]) { | |
// If it\'s a logical vector and the first element is TRUE. | |
result = true; | |
} else { | |
result = false; | |
} | |
UNPROTECT(1); | |
return Rf_ScalarLogical(result); | |
' | |
) | |
checkver() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment