インラインRCPPを使用してグローバル環境から変数を読みますか?

StackOverflow https://stackoverflow.com/questions/3671862

  •  01-10-2019
  •  | 
  •  

質問

RCPP Intro Vignetteの例をフォローして、インラインで試してみました。

f<-cxxfunction(signature(), plugin="Rcpp", body="
    Environment global = Environment::global_env();
    std::vector<double> vx = global['x'];
")

しかし、コンパイルエラーが発生します。

file12384509.cpp: In function 'SEXPREC* file12384509()':
file12384509.cpp:31: error: invalid use of incomplete type 'struct SEXPREC'
C:/PROGRA~1/R/R-211~1.1/include/Rinternals.h:333: error: forward declaration of 'struct SEXPREC'
file12384509.cpp:31: error: conversion from 'SEXPREC' to non-scalar type 'std::vector<double, std::allocator<double> >' requested
make: *** [file12384509.o] Error 1

ERROR(s) during compilation: source code errors or compiler configuration errors!

Program source:
  1: // includes from the plugin
  2: 
  3: #include <Rcpp.h>
  4: 
  5: 
  6: #ifndef BEGIN_RCPP
  7: #define BEGIN_RCPP
  8: #endif
  9: 
 10: #ifndef END_RCPP
 11: #define END_RCPP
 12: #endif
 13: 
 14: using namespace Rcpp;
 15: 
 16: 
 17: // user includes
 18: 
 19: 
 20: // declaration
 21: extern "C" {
 22: SEXP file12384509( ) ;
 23: }
 24: 
 25: // definition
 26: 
 27: SEXP file12384509(  ){
 28: BEGIN_RCPP
 29: 
 30: Environment global = Environment::global_env();
 31: std::vector<double> vx = global['x'];
 32: 
 33: END_RCPP
 34: }
 35: 
 36: 
Error in compileCode(f, code, language = language, verbose = verbose) : 
  Compilation ERROR, function(s)/method(s) not created! file12384509.cpp: In function 'SEXPREC* file12384509()':
file12384509.cpp:31: error: invalid use of incomplete type 'struct SEXPREC'
C:/PROGRA~1/R/R-211~1.1/include/Rinternals.h:333: error: forward declaration of 'struct SEXPREC'
file12384509.cpp:31: error: conversion from 'SEXPREC' to non-scalar type 'std::vector<double, std::allocator<double> >' requested
make: *** [file12384509.o] Error 1

何が悪いのか、これを修正する方法はありますか?これは単なるおもちゃの例です。これに対する答えに応じて、より重要な複雑な問題があります。

役に立ちましたか?

解決

あなたの興味に感謝します rcpp!ロマンと私は通常、RCPPデベルリストに質問が提起されることをお勧めします。あなたはおそらくそこにさらにいくつかの適切な眼球を手に入れているでしょう。

ここでは、シングルとダブルの引用符のtrapに陥りました。これらを切り替えると、すべてが機能します。また、コードを少し並べ替え /再配置 /再評価しました。

> f <- cxxfunction(signature(),
+                  body=' Environment e = Environment::global_env();  
+                         std::vector<double> vx = e["x"]; 
+                         return wrap(vx); ',
+                  plugin="Rcpp")
> x <- 3:6
> f()
[1] 3 4 5 6
> 

編集: それが価値があることについては、ここに同じですが、環境を渡します。それは私が最初に遊んだものであり、私はどういうわけかより良いのが好きです

f <- cxxfunction(signature(env="environment"),
                 body=' Environment e(env); 
                        std::vector<double> vx = e["x"];
                        return wrap(vx); ',   
                 plugin="Rcpp") 

env <- new.env()
env[["x"]] <- 1:4 
f(env) 
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top