[Rcpp-devel] Resurrecting rinside_sample9.cpp ?
Dirk Eddelbuettel
edd at debian.org
Wed Jul 23 16:15:51 CEST 2014
On 23 July 2014 at 14:41, Christian Authmann wrote:
| Hello,
|
| I'm trying to run user provided R scripts from within my C++
| applications using RInside. I have a large amount of data (larger than
| RAM) the R script can potentially access, so it's impossible to provide
| it all in a variable - I'd need to provide a callback so the R script
| can request the data it needs.
|
| The idea is to have a function similar to sample9 (minus the bug[1])
|
| Rcpp::NumericVector getdata(int id) {
| Rcpp::NumericVector vec();
| // ...
| return vec;
| }
|
| and make it available like:
|
| RInside R;
| R["getdata"] = Rcpp::InternalFunction( &getdata );
|
|
|
| As hinted in sample9, Rcpp::InternalFunction apparently really is
| broken. Instantiating it gives compiler errors very similar to those
| everyone got in Rcpp 0.11.0. [2]
|
| I've updated both Rcpp and RInside to the latest release version, but
| the errors remain.
I was about to say that need Github versions of both as the fixes went in
pretty recently:
edd at max:~$ cd git/rinside/inst/examples/standard/
edd at max:~/git/rinside/inst/examples/standard$ ./rinside_sample9
hello( 'world') = hello world
edd at max:~/git/rinside/inst/examples/standard$
but then I realized that I am seemingly behind one commit -- the very latest
fix of making rinside_sample9.cpp use a std::string.
So apologies -- I blame my recent traveling and will update the repo this
evening. For now, the repaired rinside_sample9.cpp is included below. You
will need Rcpp from Github.
| Am I doing something wrong, or is this feature fundamentally broken? Is
| there anything I can do to fix it?
Worst case you can always do what Github user 'omazapa' did and fix it in
Rcpp and/or RInside as needed :)
More seriously, the brokenness only applied to Rcpp::InternalFunction which
was more or less deprecated as Rcpp::Function worked.
| Or are there other ways to add a function to the R script's environment?
| I've read of plenty of ways using Rcpp, but those only seem to apply
| when developing R packages. I could provide an R package, include it
| from the RInside script and thus have a custom function; but the R
| package would be separate from my application and wouldn't have access
| to the data it needs to provide.
There are lots examples that ship with RInside; some use functions -- maybe
these can provide an idea.
Otherwise, "it is just R" as we embed the interpreter. Whatever you could do
with R as is you should be able to do with RInside. Which works both ways --
thing that you can't do easily with R will also be hard for RInside.
| Or should I just install older RInside/Rcpp packages where it still
| compiled, and work with those for now?
That is also always an option.
Dirk
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8; -*-
//
// Simple example showing how expose a C++ function -- no longer builds
//
// Copyright (C) 2010 Dirk Eddelbuettel and Romain Francois
#include <RInside.h> // for the embedded R via RInside
// a c++ function we wish to expose to R
std::string hello( std::string who ){
std::string result( "hello " ) ;
result += who ;
return result;
}
int main(int argc, char *argv[]) {
// create an embedded R instance
RInside R(argc, argv);
// expose the "hello" function in the global environment
R["hello"] = Rcpp::InternalFunction( &hello ) ;
// call it and display the result
std::string result = R.parseEvalNT("hello(\"world\")") ;
std::cout << "hello( 'world') = " << result << std::endl ;
exit(0);
}
|
|
|
| [1] return result.c_str(); returns a dangling pointer, since result is
| destroyed before returning.
| [2] according to
| http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2014-March/007290.html
|
| --
| Christian Authmann
| Philipps-Universität Marburg
| Fachbereich Mathematik und Informatik
| AG Datenbanksysteme
| Hans-Meerwein-Straße
| D-35032 Marburg
| _______________________________________________
| 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
--
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org
More information about the Rcpp-devel
mailing list