使用内联 Rcpp 从全局环境中读取变量? [英] Read variables from global environment with inline Rcpp?

查看:32
本文介绍了使用内联 Rcpp 从全局环境中读取变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遵循 Rcpp 介绍 Vignette 中的示例,尝试使用内联.

I'm following the example from the Rcpp intro Vignette, trying it with inline.

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

但是我得到一个编译错误.

but I get a compile error.

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

出了什么问题,有没有办法解决这个问题?这只是玩具示例,我有一个更重要的复杂问题,取决于对此的答案.

What is wrong and is there a way to fix this? This is just the toy example, I have a more important complicated problem depending on the answer to this.

推荐答案

感谢您对 的关注Rcpp!Romain 和我通常建议在 rcpp-devel 列表中提出问题;你可能会在那里得到一些更合适的眼球.

Thanks for your interest in Rcpp! Romain and I usually suggest that questions be posed on the rcpp-devel list; you are probably getting a few more appropriate eyeballs there.

在这里,您陷入了单引号和双引号的陷阱.切换这些使一切正常.我还在玩的时候对代码重新排序/重新排列/重新标记:

Here, you fell into a trap of single versus double quotes. Switching these around make it all work. I also reordered / rearranged / relabeled the code a little while I was playing with it:

> 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
> 

就其价值而言,这里是相同的,但传递了一个环境.这就是我首先玩的,而且我更喜欢它

For what it's worth, here is the same but passing an environment down. That's what I played with first and which I somehow like better

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) 

这篇关于使用内联 Rcpp 从全局环境中读取变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆