Asked 6th Sep, 2014
Deleted profile

Can anyone help me with this problem when running R scripts from C/C++?

I am working on a C++ program that depends on an R package, using Rcpp and RInside to bridge the gap. While I can execute as many lines of R as I like by calling RInside::parseEvalQ() for each line, it would be more readable and more maintainable to put the R portion of the program in a .r script. Unfortunately, I can't quite get it to work.
The naive approach, calling parseEvalQ("source('foo.r')"), looks for a file foo.r located in the program's working directory, rather than the source or installation directory. This can lead to highly unpredictable behavior, depending on where the program is being called from. The only alternative I've been able to find is to give a hardcoded path to the file, which is a portability nightmare.
Does anyone know of a way to ensure that either
  • the embedded R session searches for files relative to the C++ executable it's embedded in, or
  • the compiler and/or preprocessor somehow accounts for the contents of a specific .r file during building?
Either approach would solve my problem.

Most recent answer

17th Oct, 2014
Daniel Peralta
Ghent University
Rserve could also suit your needs. We have just tried it in our lab, calling it from PHP and Python, and it works great.

All Answers (4)

Deleted profile
Ah, it hadn't occurred to me to try to pass command line arguments to the R session. I'll give it a try. Thanks!
11th Sep, 2014
M. O. Vasil’ev
May be you may use autotools to generate config.h for you and make it generate some variable like installpath or so?

Similar questions and discussions

Related Publications

The short appendix offers a very basic introduction to the C++language to someone already (at least somewhat) familiar with Rprogramming. Introducing all of C++in just a few pages is not really possible. Countless books have been written about the C++language since its inception in the early 1990s (and we will list a few at the end in a section on...
R esum e. Nous pr esentons dans ce document, un mod ele de programmation fond e sur l'approche ob-jet, l'approche r eactive e t l a c o m m unication par diiusion. Ce nouveau mod ele permet de d ecrire na-turellement les interactions pouvant appara^ entre les objets. Nous pr esentons ennn bri evement u n e impl ementation r ealis ee avec les langag...
Got a technical question?
Get high-quality answers from experts.