[Rcpp-devel] using RApi inC/C++

nandan amar nandan.amar at gmail.com
Fri Mar 18 15:09:56 CET 2011


Dear All,
I am trying to use Some R api in c code.
To start with I copied following code and try to run as


a:~/Desktop/rcpp$ g++ file.cpp

i got following error

r.cpp:1: fatal error: R.h: No such file or directory
compilation terminated.

How should I compile the code.

file.cpp
_____________________________________________________________
#include <R.h>
//#include <Rinternals.h>
#include <RInside.h>                    // for the embedded R via RInside
 using namespace std;
int main(int argc, char *argv[]) {

   // create an embedded R instance
   RInside R(argc, argv);

   // evaluate an R expression with curve()
   // because RInside defaults to interactive=false we use a file
   std::string cmd = "tmpf <- tempfile('curve'); "
                     "png(tmpf); "
                     "curve(x^2, -10, 10, 200); "
                     "dev.off();"
                     "tmpf";
   // by running parseEval, we get the last assignment back, here the filename
   std::string tmpfile = R.parseEval(cmd);

   std::cout << "Could now use plot in " << tmpfile << std::endl;
   unlink(tmpfile.c_str());            // cleaning up

   // alternatively, by forcing a display we can plot to screen
   cmd = "x11(); curve(x^2, -10, 10, 200); Sys.sleep(30);";
   // parseEvalQ evluates without assignment
   R.parseEvalQ(cmd);

   exit(0);
}

--
Amar Kumar Nandan


More information about the Rcpp-devel mailing list