带有来自 R 的参数的 RInside 调用 [英] RInside call with arguments from R
问题描述
我正在尝试将参数传递给包含 RInside
的 exe
文件,该文件是使用 make
编译的.
I am trying to pass arguments to an exe
file that includes RInside
, and that is compiled using make
.
#include <RInside.h>
int main(int argc, char *argv[]) {
// define two vectors in C++
std::vector<double> x({1.23, 2.34, 3.45});
std::vector<double> y({2.34, 3.45, 1.23});
// start R
RInside R(argc, argv);
// define a function in R
R.parseEvalQ("rtest <- function(x, y) {x + y}");
// transfer the vectors to R
R["x"] = x;
R["y"] = y;
// call the function in R and return the result
std::vector<double> z = R.parseEval("rtest(x, y)");
std::cout << z[0] << std::endl;
// move R function to C++
Rcpp::Function rtest((SEXP) R.parseEval("rtest"));
// call the R function from C++
z = Rcpp::as<std::vector<double>>(rtest(x, y));
std::cout << z[0] << std::endl;
exit(0);
}
我有两个顾虑:
首先,尝试 make -f Makefile.win soraw
给出以下错误.为什么它不起作用?
First, trying make -f Makefile.win soraw
give the error below. why is it not working ?
soraw.cpp:21:36: error: '>>' should be '> >' within a nested template argument list
z = Rcpp::as<std::vector<double>>(rtest(x, y));
^
第二,将 x
和 y
从 R 传递给这个 c++ 代码(在编译成一个 exe 之后)而不是在 c++ 中声明它们的最佳方法是什么?代码?我应该使用文件吗?
Second, what would be the best way to pass x
and y
to this c++ code (after compiling into an exe) from R instead of declaring them in the c++ code? Should I use files?
EDIT 这是尝试添加额外空间时的错误:candidate expects 0 arguments, 1 provided
EDIT this is the error when trying to comile with an additional space: candidate expects 0 arguments, 1 provided
z = Rcpp::as<std::vector<double> >(rtest(x, y));
给予
C:/Rtools/mingw_64/x86_64-w64-mingw32/include/c++/bits/stl_vector.h:264:7: note: no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const allocator_type& {aka const std::allocator<double>&}'
C:/Rtools/mingw_64/x86_64-w64-mingw32/include/c++/bits/stl_vector.h:253:7: note: std::vector<_Tp, _Alloc>::vector() [with _Tp = double; _Alloc = std::allocator<double>]
vector()
^
C:/Rtools/mingw_64/x86_64-w64-mingw32/include/c++/bits/stl_vector.h:253:7: note: candidate expects 0 arguments, 1 provided
make: *** [<builtin>: soorig] Error 1
<小时>
这是我在 Makefile.win
推荐答案
编译的问题似乎是 Dirk 和我的 g++
默认使用 C++11,而 Rtools 的没有.您可以通过更改 RInside
附带的 GNUmakefile
中定义 CXX
和 CXXFLAGS
的方式来解决此问题:
The problem with compilation seems to be that Dirk's and my g++
default to C++11, while the one from Rtools does not. You can fix that by changing the way CXX
and CXXFLAGS
are defined in the GNUmakefile
that comes with RInside
:
CXX := $(shell $(R_HOME)/bin/R CMD config CXX11) $(shell $(R_HOME)/bin/R CMD config CXX11STD)
CPPFLAGS := -Wall $(shell $(R_HOME)/bin/R CMD config CPPFLAGS)
CXXFLAGS := $(RCPPFLAGS) $(RCPPINCL) $(RINSIDEINCL) $(shell $(R_HOME)/bin/R CMD config CXX11FLAGS)
或者,您可以从 C++11 中删除所有细节.
Alternatively you could remove all the niceties from C++11.
至于如何提供输入数据:这实际上取决于您是否要提供可能的长向量.对于前一种情况,我会使用文件.
As for how to provide the input data: That really depends whether you want to provide possibly long vectors or not. I would use files for the former case.
这篇关于带有来自 R 的参数的 RInside 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!