Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
R does not  get compiled into intermediate code. Can any one help on how to integrate R with C++.


What I have tried:

Tried running sample code to call a method from R script. Failed with linker errors.
Posted
Updated 20-Jun-17 19:30pm
Comments
Mohibur Rashid 19-Jun-17 3:05am    
have you tried google?
http://gallery.rcpp.org/articles/r-function-from-c++/
AshwiniKJ 19-Jun-17 7:39am    
I have a separate R script that is reading CSV and performing the task. Now I need to call that script/method from that script directly from my C++ code.

This is how I tried,

#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; //x == NULL; /* i.e. 0 on success */
}


Geting following linker error:

error LNK2019: unresolved external symbol _Rf_initEmbeddedR referenced in function _main
Richard MacCutchan 19-Jun-17 7:42am    
You need to add the R library that provides this function into your linker options, in the project parameters.
AshwiniKJ 19-Jun-17 7:48am    
Hi Richard,

I guess the R library does not get built while installing R in windows. Can you please guide me on how to build this library. I saw R.dll in C:\Program Files\R\R-3.3.2\bin\i386 folder. But could not find the corresponding library. I am using R on windows 2008 OS and Visual Studio for C++ compilaton.
Richard MacCutchan 19-Jun-17 10:20am    
R.dll is the library. I guess you need to go and study the documentation to find out what needs to be done to link to it.

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900