2016-11-09 75 views
0

我是Rcpp的新手,我試圖編寫一個簡單的函數,該函數將獲取字符向量列表(myList)和一個單獨的字符向量(vec1),並返回交集列表每個列表向量和vec1之間。在R中使用與Rcpp相交

我在R中可以很輕鬆地做到這一點:

myList <- list(
       c('apple','banana','orange','pineapple'), 
       c('pear','pineapple','watermelon'), 
       c('orange','apple','pineapple'), 
       NA, 
       c('watermelon','lime','apple','banana')) 

vec1 <- c('apple','pineapple','banana') 

# R implementation 
lapply(myList, function(x) intersect(x, vec1)) 

但我很困惑,如何與RCPP實現這一點。這裏是我的嘗試:

cppFunction(' 
    List intersectList (List input, StringVector vec) { 

     int n = input.size(); 
     List out(n); 

     for (int i = 0; i < n; i++) { 

      StringVector sp = Rcpp::as< std::vector<std::string> > (input[i]); 

      if (sp.length() > 0) { 
       out[i] = intersect(sp, vec); 
      } 
     } 

     return out; 

    } 
') 

......然而,這導致一個錯誤:

Error in sourceCpp(code = code, env = env, rebuild = rebuild, cacheDir = cacheDir, : 
    Error 1 occurred building shared library. 
file1510d6dbd767c.cpp:14:17: error: no viable conversion from 'std::__1::vector<std::__1::basic_string<char>, std::__1::allocator<std::__1::basic_string<char> > >' to 'StringVector' (aka 'Vector<16>') 
         StringVector sp = Rcpp::as< std::vector<std::string> > (input[i]); 
            ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/Rcpp/include/Rcpp/vector/Vector.h:65:5: note: candidate constructor not viable: no known conversion from 'std::__1::vector<std::__1::basic_string<char>, std::__1::allocator<std::__1::basic_string<char> > >' to 'const Rcpp::Vector<16, PreserveStorage> &' for 1st argument 
    Vector(const Vector& other){ 
    ^
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/Rcpp/include/Rcpp/vector/Vector.h:73:5: note: candidate constructor not viable: no known conversion from 'std::__1::vector<std::__1::basic_string<char>, std::__1::allocator<std::__1::basic_string<char> > >' to 'SEXP' (aka 'SEXPREC *') for 1st argument 
    Vector(SEXP x) { 
    ^
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/Rcpp/include/Rcpp/vector/Vector.h:101:5: note: candidate constructor not viable: no known conversion from 'std::__1::vector<std::__1::basic_string<char>, std::__1::allocator<std::__1::basic_string<char> > >' to 'const std::string &' (aka 'const basic_string<char, char_traits<char>, allocator<char> > &') for 1st argument 
    Vector(const std::string& st){ 
    ^
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/Rcpp/include/Rcpp/vector/Vector.h:107:5: note: candidate constructor not viable: no known conversion from 'std::__1::vector<std::__1::basic_string<char>, std::__1::allocator<std::__1::basic_string<char> > >' to 'const char *' for 1st argument 
    Vector(const char* st) { 
    ^
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/Rcpp/include/Rcpp/vector/Vector.h:128:5: note: candidate constructor not viable: no known conversion from 'std::__1::vector<std::__1::basic_string<char>, std::__1::allocator<std::__1::basic_string<char> > >' to 'const int &' for 1st argument 
    Vector(const int& size) { 
    ^
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/Rcpp/include/Rcpp/vector/Vector.h:133:5: note: candidate constructor not viable: no known conversion from 'std::__1::vector<std::__1::basic_string<char>, std::__1::allocator<std::__1::basic_string<char> > >' to 'const Rcpp::Dimension &' for 1st argument 
    Vector(const Dimension& dims) { 
    ^
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/Rcpp/include/Rcpp/vector/Vector.h:78:5: note: candidate template ignored: could not match 'GenericProxy' against 'vector' 
    Vector(const GenericProxy<Proxy>& proxy){ 
    ^
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/Rcpp/include/Rcpp/vector/Vector.h:123:42: note: candidate template ignored: disabled by 'enable_if' [with T = std::__1::vector<std::__1::basic_string<char>, std::__1::allocator<std::__1::basic_string<char> > >] 
     typename Rcpp::traits::enable_if<traits::is_arithmetic<T>::value, void>::type* = 0) { 
             ^
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/Rcpp/include/Rcpp/vector/Vector.h:146:45: note: candidate template ignored: disabled by 'enable_if' [with T = std::__1::vector<std::__1::basic_string<char>, std::__1::allocator<std::__1::basic_string<char> > >] 
      typename Rcpp::traits::enable_if<traits::is_bool<T>::value && RTYPE == LGLSXP, void>::type* = 0) { 
              ^
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/Rcpp/include/Rcpp/vector/Vector.h:162:5: note: candidate template ignored: could not match 'VectorBase' against 'vector' 
    Vector(const VectorBase<RTYPE,NA,VEC>& other) { 
    ^
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/Rcpp/include/Rcpp/vector/Vector.h:176:5: note: candidate template ignored: could not match 'SingleLogicalResult' against 'vector' 
    Vector(const sugar::SingleLogicalResult<NA,T>& obj) { 
    ^
1 error generated. 
make: *** [file1510d6dbd767c.o] Error 1 

在這種特定情況下,R實現簡單,速度快,所以RCPP可能沒有必要。但我想了解Rcpp的實現,因爲它會讓我更好地理解如何使用Rcpp處理向量和列表。

回答

2

簡言之:

  1. 該函數的調用intersect;你的標題談到intercept。不是一回事。

  2. intersect()的單元測試是單線測試(見下文)。如果我是你,我會從它開始,並嘗試StringVector類型。

  3. 您正在跳躍之間StringVectorstd::vector<std::string>之間。爲什麼?如果您更改StringVector sp = Rcpp::as< std::vector<std::string> > (input[i]);以在as<>()中使用StringVector,那麼您可能會很好。 [未經測試,現在沒時間。 ]

測試功能(在文件unitTests/cpp/sugar.cpp包括在RCPP的每個版本是:

// [[Rcpp::export]] 
IntegerVector runit_intersect(IntegerVector x, IntegerVector y){ 
    return intersect(x, y) ; 
} 
+0

由於(1)是我的一個拼寫錯誤,我應該仔細閱讀。 我來自純粹的R背景,對於Rcpp(和C++)來說是新的。到目前爲止,我一直都不清楚這種差異是tween StringVector,CharacterVector和std :: vector 。 但這有助於指引我走向正確的方向!謝謝! – Pascal

0

由於德克Eddelbuettel用於建議如下:

> cppFunction(' 
+ List intersectList (List input, StringVector vec) { 
+ 
+  int n = input.size(); 
+  List out(n); 
+ 
+  for (int i = 0; i < n; i++) { 
+   
+   StringVector sp = Rcpp::as<StringVector> (input[i]); 
+ 
+   if (sp.length() > 0) { 
+    out[i] = intersect(sp, vec); 
+   } 
+  } 
+ 
+  return out; 
+ 
+ } 
+ ') 
> 
> myList <- list(
+    c('apple','banana','orange','pineapple'), 
+    c('pear','pineapple','watermelon'), 
+    c('orange','apple','pineapple'), 
+    NA, 
+    c('watermelon','lime','apple','banana')) 
>    
> vec1 <- c('apple','pineapple','banana') 
> 
> intersectList(myList, vec1) 
[[1]] 
[1] "pineapple" "banana" "apple"  

[[2]] 
[1] "pineapple" 

[[3]] 
[1] "pineapple" "apple"  

[[4]] 
character(0) 

[[5]] 
[1] "banana" "apple"