简体   繁体   English

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

[英]Calling R function from C++ on Windows

I am trying to call R functions from C++ on Windows. 我试图在Windows上从C ++调用R函数。 I am using MinGW for compiling the program, but it throws error while compiling. 我正在使用MinGW来编译程序,但它在编译时会抛出错误。 Code (taken from Dirk) and compilation error are as follows: 代码(取自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

Any ideas what I am missing? 我缺少什么想法?

Jim is correct in his earlier answer, but there is more. 吉姆在他之前的回答中是正确的,但还有更多。

When using RInside , you also need to 使用RInside时 ,您还需要

  1. include and link with Rcpp (which RInside depends upon) a 包含并链接RcppRInside依赖)a

  2. include and link with R (which both depends upon) as well as of course 包括和链接R(两者都取决于)以及当然

  3. RInside 's own library RInside自己的图书馆

The easiest way of doing this is to simply use the Makefile from the examples/standard directory --- given that you copied the code of one of the examples, you should also copy the build instructions. 最简单的方法是简单地使用examples/standard目录中的Makefile ---假设您复制了其中一个示例的代码,您还应该复制构建指令。

Lastly, and that is your biggest issue: RInside applications do not currently work on Windows, which is clearly documented on the RInside page. 最后,这是您最大的问题:RInside应用程序目前无法在Windows上运行,这在RInside页面上有明确说明。 It will build, but segfault on startup. 它会构建,但启动时会出现段错误。 Debugging help would be appreciated, this works on OS X and Linux. 调试帮助将不胜感激,这适用于OS X和Linux。

你实际上并没有在R库中链接(无论是什么)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM