[Rcpp-devel] using RInside calls in a subroutine?

Dirk Eddelbuettel edd at debian.org
Tue Dec 7 19:45:07 CET 2010


Eileen,

To make this a little more instructive, please find a repaired single-file
version of your program below.  You can of course split off a header file and
a file for the function but there is no need.  Below it is a quick Makefile
based on the aforementioned Makefile from the RInside examples.

With that:

edd at max:/tmp/eileen$ 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   -c -o main.o main.cpp
cc   main.o  -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 main
edd at max:/tmp/eileen$ ./main
1
2.2
3.4
4.33
42.4
Running ls()
[1] "argv" "x"    "y"   
In main_stat_compare, ks p-value is 0.873016
Running ls()
[1] "argv" "ks"   "x"    "y"   
In main_stat_compare, ks p-value is 0.873016
edd at max:/tmp/eileen$ 

Cleanly calls twice, just as we suspected. 

Cheers, Dirk


#include <stdio.h>
#include <RInside.h>

std::vector< double > ks_test(std::vector< double >x, std::vector<double >y, RInside & R) {

   SEXP ans;

   R.assign(x,"x");
   R.assign(y, "y");

   std::string evalstr = " \
     cat('Running ls()\n'); print(ls());    \
     ks <- ks.test(x,y);            \
     ks$p";

   R.parseEval(evalstr, ans);
   Rcpp::NumericVector vec(ans); // new API 
   std::vector<double> v = Rcpp::as<std::vector<double> >(vec); // use as() to convert
   return(v);
}

int main(int argc, char *argv[]) {

   RInside::RInside R(argc,argv);
   // testing stuff
   std::vector< double > x(5);
   std::vector< double > y(5);
   int i;
   double var[5] = {1,2.2,3.4,4.33,42.4};
   for(i=0; i<5; i++) {
     x[i] = var[i];
     std::cout << x[i] << std::endl;
   }

   double var2[5] = {3.224,4.1,4.22,4,4.4};
   for(i=0; i<5; i++) {
     y[i] = var2[i];
   }

   // correct function call
   std::vector< double > v = ks_test(x,y,R);

   // output
   std::cout << "In main_stat_compare, ks p-value is " << v[0] << std::endl;

   // and call again
   v = ks_test(x,y,R);

   // output
   std::cout << "In main_stat_compare, ks p-value is " << v[0] << std::endl;

   exit(0);
}



// --------------- Makefile below


## -*- mode: make; tab-width: 8; -*-
##
## Simple Makefile
##
## TODO: 
##  proper configure for non-Debian file locations,   [ Done ]
##  allow RHOME to be set for non-default R etc

## comment this out if you need a different version of R, 
## and set set R_HOME accordingly as an environment variable
R_HOME := 		$(shell R RHOME)

sources := 		$(wildcard *.cpp)
#programs := 		$(sources:.cpp=)


## include headers and libraries for R 
RCPPFLAGS := 		$(shell $(R_HOME)/bin/R CMD config --cppflags)
RLDFLAGS := 		$(shell $(R_HOME)/bin/R CMD config --ldflags)
RBLAS := 		$(shell $(R_HOME)/bin/R CMD config BLAS_LIBS)
RLAPACK := 		$(shell $(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
RCPPINCL := 		$(shell echo 'Rcpp:::CxxFlags()' | $(R_HOME)/bin/R --vanilla --slave)
RCPPLIBS := 		$(shell echo 'Rcpp:::LdFlags()'  | $(R_HOME)/bin/R --vanilla --slave)


## include headers and libraries for RInside embedding classes
RINSIDEINCL := 		$(shell echo 'RInside:::CxxFlags()' | $(R_HOME)/bin/R --vanilla --slave)
RINSIDELIBS := 		$(shell echo 'RInside:::LdFlags()'  | $(R_HOME)/bin/R --vanilla --slave)

## compiler etc settings used in default make rules
CXX := 			$(shell $(R_HOME)/bin/R CMD config CXX)
CPPFLAGS := 		-Wall $(shell $(R_HOME)/bin/R CMD config CPPFLAGS)
CXXFLAGS := 		$(RCPPFLAGS) $(RCPPINCL) $(RINSIDEINCL) $(shell $(R_HOME)/bin/R CMD config CXXFLAGS)
LDLIBS := 		$(RLDFLAGS) $(RRPATH) $(RBLAS) $(RLAPACK) $(RCPPLIBS) $(RINSIDELIBS)

all: 			main

main:			main.o 






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


More information about the Rcpp-devel mailing list