Interfacing C and R: The .Call() Interface
Jan de Leeuw
Version 004 08/25/2016
1 IntroductionNote: The directory gifi.stat.ucla.edu/dotcall has the complete Rmd file of this report with all code chunks. It also has pdf and epub versions, and R and C files with the code. The book and the files that go with it are in the public domain. Suggestions for improvement are always welcome.
The model for using R (R Core Team (2016)) in this report is:
- We input the data and store them in objects in R.
.Call()we pass the R objects to C subroutines for computation.
- We get the results back as R objects from C.
- We use R again to generate output, including graphics.
This implies, or at least suggests, the following conventions.
- All I/O is handled in R.
- All type checking, and argument coercion is done in R.
Rcppin this report.
- Our C functions should not have side effects.
I do not use
Rcpp (Eddelbuettel (2013)) because in my formative years I was taught to dislike
C++, and, even after years of therapy, I never got over those negative feelings. This makes the material in this book somewhat old-fashioned, since the excellent and authorative new books by Wickham (2015) and Chambers (2016) clearly suggest using
Rcpp. Thus my stubbornness is not related to any shortcomings of
Rcpp. It merely illustrates that because I am already retired, I have nothing to prove and nothing to lose.
As for coding style in both R and C:
- spaces around binary operators except in x:y,
- braces for groups of statements (even if there is only a single statement in the group) with the closing brace is on its own line,
- no semi-colons, i.e no multiple statements on a single line,
- spaces after commas,
- spaces before opening parentheses, except in function or macro calls,
- no spaces for square brackets indexing vectors, matrices, or lists,
- lower case, possibly with underscores, for variable and function names.
See Wickham (2015), chapter 5, for more details.
R Core Team. 2016. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.
Eddelbuettel, D. 2013. Seamless R and C++ Integration with Rcpp. Use R! Springer.
Wickham, H. 2015. Advanced R. The R Series. CRC Press.
Chambers, J.M. 2016. Extending R. The R Series. CRC Press.