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

Dirk Eddelbuettel edd at debian.org
Fri Mar 18 15:39:24 CET 2011


On 18 March 2011 at 19:39, nandan amar wrote:
| 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.

Please look at the Makefile in the examples/standard/ directory of RInside.

I just dropped your code as file 'amar.cpp' and said 'make'. Here is what
happens: 

edd at max:~/svn/rinside/pkg/inst/examples/standard$ make 
g++ -I/usr/share/R/include -I/usr/local/lib/R/site-library/Rcpp/include -I"/usr/local/lib/R/site-library/RInside/include" -O3 -pipe -g -Wall    amar.cpp  -L/usr/lib64/R/lib -lR  -lblas -llapack -L/usr/local/lib/R/site-library/Rcpp/lib -lRcpp -Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib -L/usr/local/lib/R/site-library/RInside/lib -lRInside -Wl,-rpath,/usr/local/lib/R/site-library/RInside/lib -o amar

after which I can just say

edd at max:~/svn/rinside/pkg/inst/examples/standard$ ./amar 

and the plot appears.  You don't need the unlink(), R does that for you
(which is why we use its tempfile() functions).

Dirk

 
| 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
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list