【问题标题】:why can't convert SEXP to Rcpp::NumericMatrix?为什么不能将 SEXP 转换为 Rcpp::NumericMatrix?
【发布时间】:2013-08-24 02:07:37
【问题描述】:

我使用 R by rinside

std::string cmd = "m <-matrix(c(1:9),nrow=3))";
SEXP proxy =  m_R.parseEvalNT(cmd);
Rcpp::NumericMatrix m(proxy); 

运行时以不寻常的方式终止它

任何人请帮助我 谢谢

【问题讨论】:

  • 你的代码解析不正确,试试parse( text = "m &lt;-matrix(c(1:9),nrow=3))" )in R.

标签: rcpp rinside


【解决方案1】:

奇怪。我玩过这个,也遇到了一些运行时错误;现在它们不再出现。试试这个:

// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4;  tab-width: 8; -*-
//
// cf http://stackoverflow.com/questions/18403402/
//
// GPL'ed 

#include <RInside.h>                    // for the embedded R via RInside

int main(int argc, char *argv[]) {

    RInside R(argc, argv);              // create an embedded R instance 

    std::string cmd = "m <- matrix(c(1:9),nrow=3)";
    Rcpp::NumericMatrix M = R.parseEval(cmd);  // assign mat. M to NumericMatrix
    int n = M.nrow();
    int k = M.ncol();
    std::cout << "All good:" << n << " by " << k << std::endl;

    exit(0);
}

edd@max:~/svn/rinside/pkg/inst/examples/standard$ ./so20130823 
All good:3 by 3
edd@max:~/svn/rinside/pkg/inst/examples/standard$ 

我刚刚复制到 examples/standard/ 目录中,作为对 make 的简单调用将构建它。

【讨论】:

  • 我发现我弄错了,std::string cmd = "m
  • 多哈。当我复制它时,我也被它咬了。请参阅 rinside_sample2.cpprinside_sample3.cpp 两者都使用矩阵。哦,请随意“接受”和/或“投票”这个答案,因为 StackOverflow 网站就是这样工作的。
  • 我是第一次使用StackOverflow,我已经接受了答案。再次感谢您
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-09
  • 2016-11-09
  • 2020-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多