【问题标题】:RInside call with arguments from RRInside 使用来自 R 的参数调用
【发布时间】:2019-02-26 02:47:33
【问题描述】:

我正在尝试将参数传递给包含RInsideexe 文件,该文件是使用make 编译的。

通过使用受here.启发的代码

#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 给出以下错误。为什么它不起作用?

soraw.cpp:21:36: error: '>>' should be '> >' within a nested template argument list
     z = Rcpp::as<std::vector<double>>(rtest(x, y));
                                    ^

其次,将xy 从R 传递给这个c++ 代码(在编译成exe 之后)而不是在c++ 代码中声明它们的最佳方式是什么?我应该使用文件吗?


EDIT这是尝试与额外空格同时出现的错误: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修改这些行后得到的错误

【问题讨论】:

  • 尝试添加一个额外的空间:Rcpp::as&lt;std::vector&lt;double&gt; &gt;(rtest(x, y));
  • 修改为z = Rcpp::as&lt;std::vector&lt;double&gt; &gt;(rtest(x, y));后依然报同样的错误
  • 可能先赋值给一个变量。或者尝试不使用该代码,确定您实际上可以使用 RInside 进行构建。然后整理一下小问题。
  • 顺便说一句,在 Ubuntu 18.10 上使用更新的 g++ 作为 例如 g++ 8.2.0,您的代码编译得很好。
  • 您的编译器版本在 Windows 上已修复。你需要弄清楚它失败的原因。至于传递参数,这是将命令行参数传递给C程序的标准问题。有答案,但这与 Rcpp 或 RInside 无关。

标签: r rcpp rinside


【解决方案1】:

编译的问题似乎是 Dirk 和我的 g++ 默认为 C++11,而 Rtools 的则不是。您可以通过更改CXXCXXFLAGSGNUmakefile 中定义的方式来解决这个问题,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 中的所有细节。

至于如何提供输入数据:这实际上取决于您是否要提供可能的长向量。对于前一种情况,我会使用文件。

【讨论】:

  • 谢谢拉尔夫,今晚会检查一下
  • 刚刚尝试替换 Makefile.win 中的这些行 - 因为我在 Windows 上,我收到此错误 g++.exe: error: =: No such file or directory make: *** [&lt;builtin&gt;: soorig] Error 1
  • 在 GNUmakefile 中更改这些行会产生另一个错误:note: no known conversion for argument 1 from '&lt;brace-enclosed initializer list&gt;' to 'const allocator_type&amp; {aka const std::allocator&lt;double&gt;&amp;}'
  • 下一行:C:/Rtools/mingw_64/x86_64-w64-mingw32/include/c++/bits/stl_vector.h:253:7: note: std::vector&lt;_Tp, _Alloc&gt;::vector() [with _Tp = double; _Alloc = std::allocator&lt;double&gt;] vector() ^
  • @gpier 请将Makefile.win 以及您看到的错误消息添加到问题中。评论不足以提供此类信息。
猜你喜欢
  • 2014-03-23
  • 2011-07-05
  • 2020-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-14
  • 2016-01-21
  • 1970-01-01
相关资源
最近更新 更多