getObjectDims

C++ Function Reference

1 Signature

Rcpp::IntegerVector BigDataStatMeth::getObjectDims(Rcpp::RObject obj, std::string strtype)

2 Description

Gets dimensions of an R object.

3 Parameters

  • obj (Rcpp::RObject): R object to measure
  • strtype (std::string): Optional pre-determined object type

4 Returns

Rcpp::IntegerVector Two-element vector with dimensions

5 Details

objR object to measure strtypeOptional pre-determined object type Rcpp::IntegerVector Two-element vector with dimensionsReturns [1, length] for vectorsReturns [rows, cols] for matricesReturns [nrow, ncol] for data framesReturns [0, 0] for unsupported types

6 Call Graph

Function dependencies

7 Source Code

File: inst/include/Utilities/Utilities.hppLines 179-228

inline Rcpp::IntegerVector getObjectDims(Rcpp::RObject obj, std::string strtype) 
    {
        
        Rcpp::IntegerVector dims(2);
        
        try 
        {
            if(strtype =="") {
                strtype = getObjecDataType(obj);    
            }
            
            if( strtype == "numeric"  || strtype == "int" || strtype == "factor" ){
                if( Rf_isMatrix(obj)) {
                    dims[0] = Rcpp::as<Rcpp::NumericMatrix>(obj).rows();
                    dims[1] = Rcpp::as<Rcpp::NumericMatrix>(obj).cols();
                } else {
                    dims[0] = 1;
                    dims[1] = Rcpp::as<Rcpp::NumericVector>(obj).length();
                }
            }else if( strtype == "logic" ) {
                if( Rf_isMatrix(obj)) {
                    dims[0] = Rcpp::as<Rcpp::LogicalMatrix>(obj).rows();
                    dims[1] = Rcpp::as<Rcpp::LogicalMatrix>(obj).cols();
                } else {
                    dims[0] = 1;
                    dims[1] = Rcpp::as<Rcpp::LogicalVector>(obj).length();
                }
            } else if( strtype == "char" ){
                if( Rf_isMatrix(obj)) {
                    dims[0] = Rcpp::as<Rcpp::CharacterMatrix>(obj).rows();
                    dims[1] = Rcpp::as<Rcpp::CharacterMatrix>(obj).cols();
                } else {
                    dims[0] = 1;
                    dims[1] = Rcpp::as<Rcpp::CharacterVector>(obj).length();
                }
            } else if(strtype == "dataframe"){
                dims[0] = Rcpp::as<Rcpp::DataFrame>(obj).nrows();
                dims[1] = Rcpp::as<Rcpp::DataFrame>(obj).length();
                
            } else {
                dims[0] = 0;
                dims[1] = 0;
            }
            
        } catch(std::exception& ex) {
            Rcpp::Rcout<< "c++ exception getObjecDataType: "<<ex.what()<< " \n";
        }
        
        return(dims);
    }

8 Usage Example

#include "BigDataStatMeth.hpp"

// Example usage
auto result = getObjectDims(...);