0

Is it possible to check if a name exists in a List object that's passed to a function in Rcpp?

In base R this can be checked in the following manner:

"a" %in% names(foo)

albeit, noting the difficulties in such an approach (here). Is there an equivalent approach for an Rcpp function? Thanks.

p-robot
  • 4,652
  • 2
  • 29
  • 38

1 Answers1

2

Here is a very simple first pass. You can use STL members of vectors of string for the matches too and more.

Code

#include <Rcpp.h>                                                                                                                                             
                                                                                                                                                              
// [[Rcpp::export]]                                                                                                                                           
bool contains(std::string s, Rcpp::List L) {                                                                                                                  
    Rcpp::CharacterVector nv = L.names();                                                                                                                     
    for (int i=0; i<nv.size(); i++) {                                                                                                                         
        if (std::string(nv[i]) == s) {                                                                                                                        
            return true;                                                                                                                                      
        }                                                                                                                                                     
    }                                                                                                                                                         
    return false;                                                                                                                                             
}                                                                                                                                                             
                                                                                                                                                              
/*** R                                                                                                                                                        
l <- list(aa="a", bb=1.23, cc=LETTERS[1:4])                                                                                                                   
contains("aaa", l) # false                                                                                                                                    
contains("bb", l)  # true                                                                                                                                     
*/

Output

 > Rcpp::sourceCpp("~/git/stackoverflow/68397853/answer.cpp")                                                                                                 
                                                                                                                                                              
 > l <- list(aa="a", bb=1.23, cc=LETTERS[1:4])                                                                                                                
                                                                                                                                                              
 > contains("aaa", l) # false                                                                                                                                 
 [1] FALSE                                                                                                                                                    
                                                                                                                                                              
 > contains("bb", l)  # true                                                                                                                                  
 [1] TRUE                                                                                                                                                     
 >    

I have a vague feeling I may have answered a similar question before, and/or written similar helper functions. Anyway, takes about a minute to do a simple one so if in doubt just do it.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Excellent. Thank so much, @Dirk. I wasn't sure if I'd have to go the helper route or not but, as you say, it's straight-forward enough to just do it. Very much appreciated. – p-robot Jul 15 '21 at 17:56
  • 2
    I think the helper function you're looking for is [`containsElementNamed()`](https://stackoverflow.com/a/22893766/5977215) – SymbolixAU Jul 16 '21 at 04:37
  • 1
    Indeed! Good to know that at least someone around here knows the full breadth and depth of Rcpp ;-) – Dirk Eddelbuettel Jul 16 '21 at 13:42