I have a third-party C library I am using to write an R extension. I am required to create a few structs defined in the library (and initialize them) I need to maintain them as part of an S4 object (think of these structs as defining to state of a computation, to destroy them would be to destroy all remaining computation and the results of all that has been already computed).
I am thinking of creating a S4 object to hold pointers these structs as void*
pointers but it is not at all clear how to do so, what would be the type of the slot?
S4 object with a pointer to a C struct
485 views Asked by pbhowmick At
1
As pointed out by @hrbrmstr, you can use the
externalptr
type to keep such objects "alive", which is touched on in this section of Writing R Extensions, although I don't see any reason why you will need to store anything asvoid*
. If you don't have any issue with using a little C++, the Rcpp classXPtr
can eliminate a fair amount of the boilerplate involved with managingEXTPTRSXP
s. As an example, assume the following simplified example represents your third party library's API:When working with pointers created via
new
it is generally sufficient to useRcpp::XPtr<SomeClass>
, because the default finalizer simply callsdelete
on the held object. However, since you are dealing with a C API, we have to supply the (default) template parameterRcpp::PreserveStorage
, and more importantly, the appropriate finalizer (free_CStruct
in this example) so that theXPtr
does not calldelete
on memory allocated viamalloc
, etc., when the corresponding R object is garbage collected.Continuing with the example, assume you write the following functions to interact with your
CStruct
:At this point, you have done enough to start handling
CStructs
from R:ptr <- MakeCStruct()
will initialize aCStruct
and store it as anexternalptr
in RUpdateCStruct(ptr, x)
will modify the data stored in theCStruct
,SummarizeCStruct(ptr)
will print a summary, etc.rm(ptr); gc()
will remove theptr
object and force the garbage collector to run, thus callingfree_CStruct(ptr)
and destroying the object on the C side of things as wellYou mentioned the use of S4 classes, which is one option for containing all of these functions in a single place. Here's one possibility:
Then, we can work with the
CStruct
s like this:Of course, another option is to use Rcpp Modules, which more or less take care of the class definition boilerplate on the R side (using reference classes rather than S4 classes, however).