【问题标题】:Integrating R code from C++从 C++ 集成 R 代码
【发布时间】:2017-06-20 17:30:49
【问题描述】:

我想使用 Visual Studio 从 C++ 代码调用 R 函数。代码如下。我收到以下错误:

“错误 LNK2019:未解析的外部符号 _Rf_initEmbeddedR 在函数 _main 中引用”

我找不到"_Rf_initEmbeddedR" 的定义。任何人都可以帮助如何将 R 与 C++ 代码集成?

enter code here
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include "Rinternals.h"
#include "Rembedded.h" 

SEXP hello() {
  return mkString("Hello, world!\n"); 
} 

int main(int argc, char **argv) 
{ 
  SEXP x; 
  Rf_initEmbeddedR(argc, argv); 
  x = hello(); 
  return 0;          
}

【问题讨论】:

  • @Sergey:我看到堆栈溢出中的一个线程,其中有 Sergey 的评论说他能够构建 R.lib 并将其链接到 Visual Studio。任何人都可以详细说明它。链接:stackoverflow.com/questions/2463437/…

标签: c++ r dll


【解决方案1】:

我们可以从 C++ 代码中调用 R 脚本,如下所示,

#include "stdafx.h"
#include <iostream>
using namespace std;

int main(int argc, char **argv) { 
cout<<"Before"<<endl;
system("C:\\\"Program Files\"\\R\\R-3.3.2\\bin\\rscript C:\\R-
Scripts\\test_script.R");

return 0;

}

rscript 是执行 test_script.R 的命令。

【讨论】:

  • 您必须避免使用这种方法:这会在您的代码中集成一个巨大的漏洞,因为任何人都可以替换文件rscript
猜你喜欢
  • 1970-01-01
  • 2012-03-04
  • 1970-01-01
  • 2012-05-15
  • 2010-12-27
  • 1970-01-01
  • 2011-02-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多