Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rcpp check if list has an element

Tags:

rcpp

My program deals with clustering. Apart from dataset user has to specify some details concerning clusters. There are two ways to approach that: you specify number of clusters or prepare list of clusters' descriptions.

args <- list(dataset=points, K=5)
args <- list(dataset=points, clusters=list(
                             list(type="spherical",radius=4),
                             list(type="covariance",covMat=matrix)
                                          )

next you call proper function (my program) in R with args as the argument.

classification <- CEC(args)

I would like to prepare CEC like below

SEXP CEC(SEXP args) {
  Rcpp::List list(args);
  arma::mat dataset = Rcpp::as<arma::mat>(list["dataset"]);
  if(list.contains("K")) {
    //something
  } else if(list.contains("clusters")) {
    //something
  }
}

I cannot find any API for List or example how to do that. Moreover, I study headers of Rcpp but definition of List which is typedef Vector<VECSXP> List ; is hardly helpful.

Is there anything I can use instead of list.contains() ?

like image 251
lord.didger Avatar asked Dec 09 '22 08:12

lord.didger


1 Answers

You are probably looking for the containsElementNamed method:

Rcpp::List list(args);
if( list.containsElementNamed("K") ){
    // something
} else {
    // something else
}

https://github.com/RcppCore/Rcpp/blob/master/inst/include/Rcpp/vector/Vector.h#L584

like image 77
Romain Francois Avatar answered Jan 12 '23 17:01

Romain Francois