beandip beandip - 3 months ago 10
R Question

What does the "install" function mean in C syntax?

I'm trying to understand the C code called by the R package ucminf. The following code is from the file interface.c found at https://cran.r-project.org/src/contrib/ucminf_1.1-4.tar.gz:

void installPar(int nn, double x[], SEXP rho) {
int i;
SEXP PAR = findVarInFrame(rho, install(".x"));
double *xpt = REAL(PAR);
if (LENGTH(PAR) != nn)
error("Dimension mismatch, length(.x) = %d != n = $d", LENGTH(PAR), nn);
for (i = 0; i < nn; i++) xpt[i] = x[i] ;
}


rho
is an environment created in R, and it contains the vector
.x
. My best guess is that the line
SEXP PAR = findVarInFrame(rho, install(".x"));
is setting
PAR
equal to
.x
, but what does the
install()
command do?

This is such a simple question that I was surprised I couldn't find the answer online - searching for "install c syntax" turned up lots of information about how to install compilers, but I couldn't find anything about the command. Any suggestions for keywords to make my searches more effective would be appreciated.

Answer

This code is part of an R extension I thing, and therefore the use of install here is a function call to the R C API for Writing Extension. What this does is create the symbol .x in the current symbol table (or return a reference to the existing .x symbol). The linked document does indicate that using install is harmless if the symbol already exists, and is a good way of looking up the symbol if that's what you actually want to do.