在 Windows 上从 C++ 调用 R 函数

2024-04-03

我正在尝试在 Windows 上从 C++ 调用 R 函数。我正在使用 MinGW 来编译程序,但它在编译时抛出错误。代码(取自Dirk)和编译错误如下:

#include <iostream>
using namespace std;
#include "RInside.h"  // for the embedded R via RInside


Rcpp::NumericMatrix createMatrix(const int n) {
  Rcpp::NumericMatrix M(n,n);
  for (int i=0; i<n; i++) {
    for (int j=0; j<n; j++) {
      M(i,j) = i*10+j;
    }
  }
  return(M);
}

int main(int argc, char *argv[]) {
  const int mdim = 4;                         // let the matrices be 4 by 4
  SEXP ans;


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

  Rcpp::NumericMatrix M = createMatrix(mdim); // create and fill a sample data Matrix
  R["M"] = M;                                 // assign C++ matrix M to R's 'M' var

  std::string evalstr = "\
            cat('Running ls()\n'); print(ls());                    \
            cat('Showing M\n'); print(M);                          \
            cat('Showing colSums()\n'); Z <- colSums(M); print(Z); \
            Z";                     // returns Z

  ans = R.parseEval(evalstr);                 // eval the init string -- Z is now in ans

  Rcpp::NumericVector v(ans);                 // convert SEXP ans to a vector of doubles
  for (int i=0; i< v.size(); i++) {           // show the result
    std::cout << "In C++ element " << i << " is " << v[i] << std::endl;
  }

  return 0;
}

Compile:

g++ -I "C:\ProgramFiles\R\R-2.14.0\library\RInside\include" -I "C:\Progra
mFiles\R\R-2.14.0\library\Rcpp\include" -I "C:\ProgramFiles\R\R-2.14.0\include"
RFunctions.cpp -o sh1.exe

Error:

C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text+0x19a): und
efined reference to `RInside::RInside(int, char const* const*, bool)'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text+0x1ee): und
efined reference to `RInside::operator[](std::string const&)'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text+0x26d): und
efined reference to `RInside::parseEval(std::string const&)'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text+0x35b): und
efined reference to `RInside::~RInside()'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text+0x3e1): und
efined reference to `RInside::~RInside()'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp7RO
bjectC2Ev[Rcpp::RObject::RObject()]+0x8): undefined reference to `vtable for Rcp
p::RObject'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp7RO
bjectC2Ev[Rcpp::RObject::RObject()]+0xd): undefined reference to `_imp__R_NilVal
ue'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN7RInside
5ProxyD1Ev[RInside::Proxy::~Proxy()]+0xd): undefined reference to `Rcpp::RObject
::~RObject()'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EED2Ev[Rcpp::Vector<14>::~Vector()]+0x16): undefined reference to `Rcpp
::RObject::~RObject()'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EED1Ev[Rcpp::Vector<14>::~Vector()]+0x16): undefined reference to `Rcpp
::RObject::~RObject()'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EEC1EP7SEXPREC[Rcpp::Vector<14>::Vector(SEXPREC*)]+0x57): undefined ref
erence to `Rcpp::RObject::setSEXP(SEXPREC*)'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EEC1EP7SEXPREC[Rcpp::Vector<14>::Vector(SEXPREC*)]+0x66): undefined ref
erence to `Rcpp::RObject::~RObject()'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ma
trixILi14EEC1ERKiS3_[Rcpp::Matrix<14>::Matrix(int const&, int const&)]+0x2c): un
defined reference to `Rcpp::Dimension::Dimension(unsigned int const&, unsigned i
nt const&)'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZNK4Rcpp6V
ectorILi14EE4sizeEv[Rcpp::Vector<14>::size() const]+0x10): undefined reference t
o `Rf_length'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6r_
castILi14EEEP7SEXPRECS2_[SEXPREC* Rcpp::r_cast<14>(SEXPREC*)]+0xd): undefined re
ference to `TYPEOF'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6r_
castILi14EEEP7SEXPRECS2_[SEXPREC* Rcpp::r_cast<14>(SEXPREC*)]+0x1d): undefined r
eference to `SEXPREC* Rcpp::internal::r_true_cast<14>(SEXPREC*)'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EEC2ERKNS_9DimensionE[Rcpp::Vector<14>::Vector(Rcpp::Dimension const&)]
+0x46): undefined reference to `Rcpp::Dimension::prod() const'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EEC2ERKNS_9DimensionE[Rcpp::Vector<14>::Vector(Rcpp::Dimension const&)]
+0x56): undefined reference to `Rf_allocVector'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EEC2ERKNS_9DimensionE[Rcpp::Vector<14>::Vector(Rcpp::Dimension const&)]
+0x67): undefined reference to `Rcpp::RObject::setSEXP(SEXPREC*)'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EEC2ERKNS_9DimensionE[Rcpp::Vector<14>::Vector(Rcpp::Dimension const&)]
+0x7d): undefined reference to `Rcpp::Dimension::size() const'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EEC2ERKNS_9DimensionE[Rcpp::Vector<14>::Vector(Rcpp::Dimension const&)]
+0xc9): undefined reference to `Rcpp::RObject::attr(std::string const&) const'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EEC2ERKNS_9DimensionE[Rcpp::Vector<14>::Vector(Rcpp::Dimension const&)]
+0x13b): undefined reference to `Rcpp::RObject::~RObject()'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZNK4Rcpp11
Environment6assignINS_6MatrixILi14EEEEEbRKSsRKT_[bool Rcpp::Environment::assign<
Rcpp::Matrix<14> >(std::basic_string<char, std::char_traits<char>, std::allocato
r<char> > const&, Rcpp::Matrix<14> const&) const]+0x23): undefined reference to
`Rcpp::Environment::assign(std::string const&, SEXPREC*) const'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp7RO
bject14AttributeProxyaSINS_9DimensionEEERS1_RKT_[Rcpp::RObject::AttributeProxy&
Rcpp::RObject::AttributeProxy::operator=<Rcpp::Dimension>(Rcpp::Dimension const&
)]+0x1c): undefined reference to `Rcpp::RObject::AttributeProxy::set(SEXPREC*) c
onst'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp8in
ternal13r_init_vectorILi14EEEvP7SEXPREC[void Rcpp::internal::r_init_vector<14>(S
EXPREC*)]+0xd): undefined reference to `double* Rcpp::internal::r_vector_start<1
4, double>(SEXPREC*)'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp8in
ternal13r_init_vectorILi14EEEvP7SEXPREC[void Rcpp::internal::r_init_vector<14>(S
EXPREC*)]+0x23): undefined reference to `Rf_length'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp8in
ternal21wrap_dispatch_unknownINS_9DimensionEEEP7SEXPRECRKT_NS_6traits17integral_
constantIbLb1EEE[SEXPREC* Rcpp::internal::wrap_dispatch_unknown<Rcpp::Dimension>
(Rcpp::Dimension const&, Rcpp::traits::integral_constant<bool, true>)]+0xd): und
efined reference to `Rcpp::Dimension::operator SEXPREC*() const'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6tr
aits14r_vector_cacheILi14EE6updateERKNS_6VectorILi14EEE[Rcpp::traits::r_vector_c
ache<14>::update(Rcpp::Vector<14> const&)]+0x15): undefined reference to `double
* Rcpp::internal::r_vector_start<14, double>(SEXPREC*)'
collect2: ld returned 1 exit status

我缺少什么想法吗?


吉姆之前的回答是正确的,但还有更多。

使用时RInside http://dirk.eddelbuettel.com/code/rinside.html,你还需要

  1. 包含并链接到Rcpp http://dirk.eddelbuettel.com/code/rcpp.html (which RInside http://dirk.eddelbuettel.com/code/rinside.html取决于)a

  2. 包含并与 R 链接(两者都依赖),当然

  3. RInside http://dirk.eddelbuettel.com/code/rinside.html自己的图书馆

最简单的方法是使用 Makefileexamples/standard目录 --- 鉴于您复制了示例之一的代码,您还应该复制构建指令。

最后,这是您最大的问题:RInside 应用程序当前无法在 Windows 上运行,这在RInside http://dirk.eddelbuettel.com/code/rinside.html页。它将构建,但启动时出现段错误。调试帮助将不胜感激,这适用于 OS X 和 Linux。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Windows 上从 C++ 调用 R 函数 的相关文章

随机推荐

  • 如何合并 Heroku 上具有相同架构的两个数据库?

    我在 Heroku 上创建了两个本质上相同的应用程序 他们开始时有所不同 因为我正在测试上传到 Heroku 并在调整时遇到一些挑战 但现在一切似乎都正常 但两者都有我想合并的数据 由于它们运行相同的 git 存储库 因此代码是相同的 迁移
  • 会话值随机消失/返回[重复]

    这个问题在这里已经有答案了 我们使用 Symfony 框架 V4 1 3 我们的会话值 看似 随机消失 重新出现 有时在几秒钟内它就消失 返回 有时它在几分钟后返回 消失 我们只是转储会话值 没有其他任何事情 到目前为止 它看起来是 100
  • 如何使用 ruby​​-on-rails 固定装置设置不属于 Rails 应用程序数据库的(外部)数据?

    根据我的要求 我创建了用于查询某些数据的外部数据库 与 Rails 应用程序使用的数据库不同 的模型 我正在尝试围绕这些模型编写测试 并希望将 样本测试数据 与实际测试分开 我以为我可以将数据放入 yml 文件并将其加载到哈希中 但它确实有
  • 给定一本字典和一个字母列表,找到可以用这些字母构建的所有有效单词

    暴力方式可以在 O n 内解决问题 基本上计算所有排列并在字典中检查结果 我正在寻找提高复杂性的方法 我可以考虑用字典构建一棵树 但仍然检查所有字母排列的时间复杂度为 O n 有更好的方法来解决这个问题吗 信件可以有重复项 该函数的 api
  • 通过代码在 Outlook 中发送/接收

    如果我使用 Excel VBA 创建 Outlook 2010 对象 Sub CreateOL On Error Resume Next Set myOlApp GetObject Outlook Application If Err Nu
  • 由于 .getContext() 未实现,单元测试引发错误

    我正在使用 Jest 为使用画布元素的组件编写测试 当我运行如下所示的测试时 我不断收到错误 Error Not implemented HTMLCanvasElement prototype getContext without inst
  • 将 System.out.println 记录到单个应用程序的日志文件

    我们在 tomcat 上有多个应用程序 它们使用 System out println 语句记录到 catalina out 有一个应用程序会创建大量日志语句 因此我想将这些应用程序输出记录到单独的日志文件中 我创建了一个 log4j xm
  • GridLayout 列宽

    我的目录中有 2 列GridLayout 我想要做的是让这些列各自占据屏幕宽度的一半 然后让它的子内容填充它们自己的单元格宽度 高度 我尝试让孩子们fill parent但这只会导致第一个占据整个布局 而且 GridLayout 似乎不支持
  • Ruby 类方法与特征类中的方法

    类方法和该类的特征类 或元类 中的方法只是定义一件事的两种方法吗 否则的话 有什么区别呢 class X class method def self a a end eigenclass method class lt lt self de
  • 我应该使用新的 HTML5 语义元素吗? [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 我正在升级我的网站 这涉及到使用新的 HTML5 语义元素的决定
  • 使用PHP和Javascript通过CORS实现站点之间的跨域通信

    经过几天的网络浏览并尝试了所有可能的方法后 我开始意识到我的主机服务器存在问题 它被我的主机服务器禁用 所以 我想把我的代码发布到 Stackoverflow 上 这样 SO 上的其他人就不会像我一样浪费时间了 该代码片段在 IE Safa
  • linux下获取硬盘剩余空间

    在 bash 脚本中 我需要获取总磁盘大小和整个磁盘当前使用的大小 我知道我可以使用以下命令获取总磁盘大小 而无需成为 root 用户 cat sys block sda size 该命令将输出设备 SDA 上的块数 将其乘以 512 您将
  • iOS——是否可以强制 UILabel 子类对象成为第一响应者?

    有什么办法可以做到这一点吗 我尝试将以下内容放入子类中 BOOL canBecomeFirstResponder return YES 但是当我向对象发送成为FirstResponder消息时 它仍然没有成为第一响应者 对的 这是可能的 你
  • 使用初始值设定项列表创建单项向量

    我有一个函数func这是超载采取任一std vector
  • MaterialDatePicker 仅选择 2 个日期之间的日期

    如何向 MaterialDatePicker 添加约束以强制用户仅选择 2 个日期之间的日期 val constraintBuilder CalendarConstraints Builder val materialRangeBuilde
  • 命令 test -x 在 ubuntu 中做什么?

    什么是test x从 etc cron daily logrotate 做什么 test x usr sbin logrotate exit 0 usr sbin logrotate etc logrotate conf 它是否测试是否已经
  • 如何加快 Julia 脚本的启动时间

    我编写了一个脚本来在 Julia 中绘制每日数据 但我发现它运行缓慢 似乎没有理想的方法来加速它 例如 foo jl bin bash exec julia O0 compile min BASH SOURCE 0 using Unicod
  • 异步 bash 脚本

    我见过一些例子 但无法将它们应用于我的情况 我有一个调用长时间运行的命令的脚本 但我想定期 例如每 1 秒 获取该调用的状态 例如 bin bash curl localhost 9200 my index forcemerge max n
  • FromBody不绑定字符串参数

    我有一个类似的问题ASP NET MVC 4 RC Web API 参数绑定问题 https stackoverflow com questions 11158617 asp net mvc 4 rc web api parameter b
  • 在 Windows 上从 C++ 调用 R 函数

    我正在尝试在 Windows 上从 C 调用 R 函数 我正在使用 MinGW 来编译程序 但它在编译时抛出错误 代码 取自Dirk 和编译错误如下 include