## Template from the example at http://dirk.eddelbuettel.com/blog/2011/03/25/#rinside_and_qt
## comment this out if you need a different version of R,
## and set set R_HOME accordingly as an environment variable
R_HOME = $$system(R RHOME)
## include headers and libraries for R
RCPPFLAGS = $$system($$R_HOME/bin/R CMD config --cppflags)
RLDFLAGS = $$system($$R_HOME/bin/R CMD config --ldflags)
RBLAS = $$system($$R_HOME/bin/R CMD config BLAS_LIBS)
RLAPACK = $$system($$R_HOME/bin/R CMD config LAPACK_LIBS)
## if you need to set an rpath to R itself, also uncomment
RRPATH = -Wl,-rpath,$$R_HOME/lib
## include headers and libraries for Rcpp interface classes
## note that RCPPLIBS will be empty with Rcpp (>= 0.11.0) and can be omitted
RCPPINCL = $$system($$R_HOME/bin/Rscript -e \"Rcpp:::CxxFlags\(\)\")
RCPPLIBS = $$system($$R_HOME/bin/Rscript -e \"Rcpp:::LdFlags\(\)\")
## include headers and libraries for RInside embedding classes
RINSIDEINCL = $$system($$R_HOME/bin/Rscript -e \"RInside:::CxxFlags\(\)\")
RINSIDELIBS = $$system($$R_HOME/bin/Rscript -e \"RInside:::LdFlags\(\)\")
## compiler etc settings used in default make rules
QMAKE_CXXFLAGS += $$RCPPWARNING $$RCPPFLAGS $$RCPPINCL $$RINSIDEINCL
QMAKE_LIBS += $$RLDFLAGS $$RBLAS $$RLAPACK $$RINSIDELIBS $$RCPPLIBS
#include <iostream>
#include <RInside.h>
using namespace std;
using namespace Rcpp;
int main()
{
RInside R; // this is an R session, yay!
R.parseEvalQ("i <- 5 + 5"); // parseEvalQ(): do stuff in the R session *Q*uietly
int x = R["i"];
cout << "Hello " << x << endl;
IntegerVector vec = R.parseEval("seq(0, 100, 7)"); // parseEval(): do stuff in R and return the results back to C++
// Access and output the Rcpp::IntegerVector:
for (unsigned i = 0; i < vec.size(); i++) {
cout << vec[i] << " ";
}
cout << endl;
// Convert IntegerVector to std::vector (useful for debugging, see explanation below the code)
vector<int> cpp_vec = as<vector<int> >(vec);
return 0;
}
In general, the syncronization of R’s random number generator with Rcpp code works like a charm. But when using this setup with RInside, it often does not work. R’s RNG always returns the first random number without changing the seed, i.e. always the same number. For development and debugging, you’ll need to manually syncronise. Just put
RNGScope scope; // needed for debugging in Qt Creator
in your Rcpp code. You can remove the line once you finished your package and use it in R directly.