How do I check if an externalptr is NULL from within R

前端 未结 2 758
心在旅途
心在旅途 2021-01-21 12:05

I\'m using SWIG to generate wrapper code to access C code from within the R language. The wrapper code uses the R externalptr type to hold references to C pointers

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-21 12:30

    For completeness, here's the solution I used. It required a function on the C side, and one on the R side, as suggested by @DirkEddelbuettel. The C function is:

    #include 
    
    SEXP isnull(SEXP pointer) {
      return ScalarLogical(!R_ExternalPtrAddr(pointer));
    }
    

    And the wrapper function in R is:

    is.null.externalptr <- function(pointer) {
      stopifnot(is(pointer, "externalptr"))
      .Call("isnull", pointer)
    }
    

    For an example of use from within R:

    > p <- new("externalptr")
    > p
    
    > is.null.externalptr(p)
    [1] TRUE
    

提交回复
热议问题