Read variables from global environment with inline Rcpp?

本小妞迷上赌 提交于 2019-12-05 06:33:23
Dirk Eddelbuettel

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
> 

Edit: 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) 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!