I’m making calls to C functions from R using R’s .Call interface. Some of the objects I am passing have custom attributes attached to them, and I want to access these attributes from C without having to pass them as separate arguments to the .Call function.
For example, consider the simple case of a real number with a custom string attribute:
x <- 1
attr(x, "myname") <- "Abiel"
One way to get at the “myname” attribute from within the C function is to pass it as a separate argument:
.Call("test", x, as.character(attr(x, "myname")))
But, I would rather just do
.Call("test", x)
and then recover the “myname” attribute within the C function. I’ve been unable to figure out how to do this with the getAttrib() function; for instance, the main line of this function below will evaluate to true, indicating a null value.
SEXP test(SEXP x)
{
isNull(getAttrib(x, mkChar("myname")));
}
Scanning the R extension writing manual has not helped me much, as all the examples of getAttrib() involve predefined symbols, such as getAttrib(x, R_DimSymbol). This section describes how to attach a custom attribute to a SEXP object you are creating in C, but not how to get at such a custom attribute that is associated with an object that is being passed into the C function.
You need to
installthe attribute to the symbol lookup table. There isn’t an example of usinggetAttribin section 5.9.4 (Attributes) of Writing R Extensions, but there are several examples in xts.h.The code below should evaluate to
FALSE.