R包开发:如何查看SEXP的类型是否为"big.matrix"?

R package development: how to check whether the type of SEXP is "big.matrix"?

我正在用低级 C 代码开发 R 包。假设我的 C 代码中有以下函数。

SEXP myFun(SEXP obj)

我需要知道 R 对象 obj 是常规 matrix 还是 big.matrix(使用 R 包 bigmemory)以便我可以调用不同的函数计算。

这可能吗?我怎么知道 obj 的 class 类型?

如果您对 C++(而不是 C)持开放态度,那么我们为您准备了两篇 Rcpp Gallery 帖子:

狭义的答案可能是在 S4 class 中测试外部指针 SEXP...但是后来我不再真正使用纯 C,因为 Rcpp 使事情变得容易得多。

您可以使用inherits函数:

#include <R.h>
#include <Rinternals.h>

SEXP myFun(SEXP obj) {
    if (inherits(obj, "big.memory")) {
        // do stuff
    }
}