1

I want to call an R function from C++ code using Visual Studio. The code is below. I am getting the following error:

"error LNK2019: unresolved external symbol _Rf_initEmbeddedR referenced in function _main"

I could not find definition of "_Rf_initEmbeddedR". Can anyone help on how to integrate R with C++ code?

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;          
}
AKJ
  • 65
  • 1
  • 6
  • @Sergey: I saw one thread in stack overflow where in there is a comment from Sergey saying he is able to build R.lib and link it in visual studio. Can any one elaborate on it. Link : https://stackoverflow.com/questions/2463437/r-from-c-simplest-possible-helloworld – AKJ Jun 20 '17 at 06:14

1 Answers1

0

We can call a R script from C++ code as below,

#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 is the command that executes the test_script.R.

AKJ
  • 65
  • 1
  • 6
  • You must avoid using such a method : this integrates a HUGE breach in your code as anyone can replace the file `rscript` – Xatyrian Jun 20 '17 at 09:33