Mic*_*ker 5 c++ dictionary r map rcpp
如何将R中的地图/字典/列表作为参数传递给c ++函数?
例如,我想做类似以下的事情:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
int test(List map) {
int val = map["test"];
return(val);
}
/*** R
map <- list(test = 200, hello = "a")
test(map)
*/
Run Code Online (Sandbox Code Playgroud)
输出应该是200.
我在 Mac OS X 上有类似的问题。运行你的代码片段似乎总是返回1。但是,如果我按以下方式修改代码,它就会起作用:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
int test(List map) {
int val = as<int>( map["test"] );
return(val);
}
/*** R
map <- list(test = 200, hello = "a")
test(map)
*/
Run Code Online (Sandbox Code Playgroud)
类型推断似乎出了问题——编译器应该“知道”这一点,因为我们分配map["test"]给一个int声明的变量,它应该被转换为int,但情况似乎并非如此。因此,为了安全起见,请确保as所有来自 R 列表的内容。
另外,值得说明的是:在 R 中200是 a double; 如果你想明确地传递一个int你应该写200L。
FWIW,我正在编译clang++:
> clang++ -v
Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin12.4.0
Thread model: posix
Run Code Online (Sandbox Code Playgroud)
和
> sessionInfo()
R version 3.0.0 (2013-04-03)
Platform: x86_64-apple-darwin10.8.0 (64-bit)
locale:
[1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] Rcpp_0.10.4
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
550 次 |
| 最近记录: |