tatami_r
R bindings to tatami matrices
|
tatami_r is an header-only library for reading abstract R matrices in tatami. This allows tatami-based C++ functions to accept and operate on any matrix-like R object containing numeric data. Usage is as simple as:
And that's it, really. If you want more details, you can check out the reference documentation.
tatami_r assumes that the hosting R instance has loaded the DelayedArray package. The UnknownMatrix
getters will then use the extract_array()
and extract_sparse_array()
R functions to retrieve data from the abstract R matrix. Note that this involves calling into R from C++, so high performance should not be expected here. Rather, the purpose of tatami_r is to ensure that tatami-based functions keep working when a native representation cannot be found for a particular matrix-like object.
It is worth mentioning that the UnknownMatrix
will always call the extract_*_array()
functions, even when a native representation exists in tatami or one of its extension libraries. R package developers should use the initializeCpp()
function from the beachmat package to map an arbitrary matrix to its appropriate representation. When such mappings exist, this allows the C++ code to operate without calling back into R for maximum efficiency. If no mapping is known, beachmat will gracefully fall back to an UnknownMatrix
to keep things running.
Given a tatami_r::UnknownMatrix
or a tatami::Matrix*
that might refer to one, we can easily parallelize operations with the tatami_r::parallelize()
function. This accepts a lambda/functor with the thread ID and the range of jobs (in the example below, rows) to be processed.
Any calls to the extract_*_array()
R functions are made thread-safe by the manticore library. Developers can also access the manticore executor to safely perform their own R API calls from each thread.
Check out the comments about safe parallelization for more gory details.
tatami_r is intended to be compiled with other relevant C++ code inside an R package using Rcpp. This is most easily done by modifying the package DESCRIPTION
with:
which will automatically use the vendored copies of tatami_r (and tatami) inside the assorthead package, along with some of pre-configured macro definitions for safe parallelization in beachmat's Rtatami.h
header. Note that C++17 is required.
If assorthead or beachmat cannot be used, the R package developer will need to copy the tatami_r and tatami include/
directories into the package's inst/include
, and then add a Makevars
file like: