Chapter 18 RObject

The RObject type is a type that can be assigned to any type of object defined in Rcpp. If you do not know what type is passed to the variable at run time, you can use RObject.

18.1 Member functions

RObject has the following member functions. These member functions also exist in all other API classes (such as NumericVector) in Rcpp.

18.1.1 inherits(str)

Returns true if this object inherits the class specified by the string str.

18.1.2 slot(name)

Accesses the slot specified by the character string name if this object is S4.

18.1.3 hasSlot(name)

Returns true if there is a slot specified by the character string name.

18.1.4 attr(name)

Accesses the attribute specified by the string name.

18.1.5 attributeNames()

Return the names of all the attributes of this object as std::vector<std::string>.

18.1.6 hasAttribute(name)

Returns true if this object has an attribute with the name specified by the string name.

18.1.7 isNULL()

Returns true if this object is NULL.

18.1.8 sexp_type()

Returns SXPTYPE of this object as int. See the R internals for a list of all SEXPTYPE defined in R.

18.1.9 isObject()

Returns true if this object has a “class” attribute.

18.1.10 isS4()

Return true if this object is a S4 object.

18.2 Determining type of object assigned to RObject

One useful use of RObject is to determine the type of the object. To determine which type the value assigned to RObject, use the is<T>() function or member function isS4() isNULL().

However, matrices and factor vectors can not be determined by only using the function is<T>() because they are vectors with values are set to specific attribute. To determine them, use Rf_isMatrix() function or the Rf_isFactor() function.

The code example below shows how to determine the type using RObject.

// [[Rcpp::export]]
void rcpp_type(RObject x){
    if(is<NumericVector>(x)){
        if(Rf_isMatrix(x)) Rcout << "NumericMatrix\n";
        else Rcout << "NumericVector\n";       
    }
    else if(is<IntegerVector>(x)){
        if(Rf_isFactor(x)) Rcout << "factor\n";
        else Rcout << "IntegerVector\n";
    }
    else if(is<CharacterVector>(x))
        Rcout << "CharacterVector\n";
    else if(is<LogicalVector>(x))
        Rcout << "LogicalVector\n";
    else if(is<DataFrame>(x))
        Rcout << "DataFrame\n";
    else if(is<List>(x))
        Rcout << "List\n";
    else if(x.isS4())
        Rcout << "S4\n";
    else if(x.isNULL())
        Rcout << "NULL\n";
    else
        Rcout << "unknown\n";
}

Use as<T>() to convert RObject to another Rcpp type after determining the type.

// Converting `RObject` to `NumericVector`
RObject x;
NumericVector v = as<NumericVector>(x);