[Rcpp-devel] Resurrecting rinside_sample9.cpp ?

2014-07-23 Thread Christian Authmann

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.




Am I doing something wrong, or is this feature fundamentally broken? Is 
there anything I can do to fix it?


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.



Or should I just install older RInside/Rcpp packages where it still 
compiled, and work with those for now?




[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@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


[Rcpp-devel] Faster lookup: Named Rcpp::List or std::map?

2014-07-23 Thread Asis Hallab
Dear Rcpp-Experts,

trying to optimize my algorithm I face the question wether a lookup by
a string key like e.g.

myRcppList( my_entry ) is as fast as the lookup in std::maps?

That said, would you recommend converting myRcppList to a std::map
before searching it?

Thank you for your help in advance!
Cheers!
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


Re: [Rcpp-devel] Faster lookup: Named Rcpp::List or std::map?

2014-07-23 Thread Romain Francois
Lookup in Rcpp::List is linear on something that is quite cheap (comparing 
pointers, because strings in R are cached). 

Lookup in std::map is logarithmic on whatever it means to compare the keys (so 
in your case comparing strings). 
on top of that, copying the data across is linear in the best case scenario 
(when the data is already sorted) or N log(N) otherwise. See: 
http://www.cplusplus.com/reference/map/map/map/

Best is to try both and see which is best for your case. Don't make this just 
one dimensional and consider things like maintainability of your code, ...

Romain

Le 23 juil. 2014 à 15:30, Asis Hallab asis.hal...@gmail.com a écrit :

 Dear Rcpp-Experts,
 
 trying to optimize my algorithm I face the question wether a lookup by
 a string key like e.g.
 
 myRcppList( my_entry ) is as fast as the lookup in std::maps?
 
 That said, would you recommend converting myRcppList to a std::map
 before searching it?
 
 Thank you for your help in advance!
 Cheers!


___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


Re: [Rcpp-devel] Resurrecting rinside_sample9.cpp ?

2014-07-23 Thread Dirk Eddelbuettel

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@max:~$ cd git/rinside/inst/examples/standard/
  edd@max:~/git/rinside/inst/examples/standard$ ./rinside_sample9 
  hello( 'world') =  hello world
  edd@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@lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


Re: [Rcpp-devel] Faster lookup: Named Rcpp::List or std::map?

2014-07-23 Thread Dirk Eddelbuettel

On 23 July 2014 at 15:30, Asis Hallab wrote:
| Dear Rcpp-Experts,
| 
| trying to optimize my algorithm I face the question wether a lookup by
| a string key like e.g.
| 
| myRcppList( my_entry ) is as fast as the lookup in std::maps?
| 
| That said, would you recommend converting myRcppList to a std::map
| before searching it?

Why don't you time / profile both and pick the faster one?

Profiling is generally a good idea.  Bottlenecks are sometimes seen in
corners you least suspected.

Dirk

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel