[Rcpp-devel] how to create an Rcpp::List in a standalone C++ application?
Florian Oswald
florian.oswald at gmail.com
Wed Dec 18 16:53:43 CET 2013
Thanks Dirk and Romain,
Let me follow up on this if you don't mind, there's probably a lot of ways
to improve what I'm doing. Dirk, you mentioned blitz++. What I'm doing at
the moment is a bit contrived and I'm wondering whether there may be a
better way of going back and forth from R to blitz. my program looks like
this:
#include <blitz/array.h>
//' R interface to 3D Blitz++
//'
// [[Rcpp::export]]
NumericVector BlitzFun( NumericVector a_ , IntegerVector d){
// map NumericVector to blitz++ array
blitz::Array<double,3> a(a_.begin(), blitz::shape(d(0),d(1),d(2))
,blitz::neverDeleteData );
// do some blitz
blitz::Array<double,3> b(a + 1.2);
//return wrap(b); does not work - obviously. would a wrap() method be
complicated?
// instead copy back to a NumericVector
NumericVector result(b.size());
result = b;
// and attach dimension attr
result.attr("dim") = d;
return result;
}
BlitzFun(1:8,c(2,2,2))
, , 1
[,1] [,2]
[1,] 2.2 4.2
[2,] 3.2 5.2
, , 2
[,1] [,2]
[1,] 6.2 8.2
[2,] 7.2 9.2
thanks
florian
On 18 December 2013 12:51, Dirk Eddelbuettel <edd at debian.org> wrote:
>
> Hi Florian,
>
> On 18 December 2013 at 11:12, Florian Oswald wrote:
> | I have a C++ class that I build and test without the help of Rcpp.
> |
> | I use Rcpp as an interface, inputting and outputting data from and to the
> | class. I'm stuck at the outputter. Ideally I would like to have this at
> the end
> | of my program:
> |
> | Rcpp::List Results = MyClass.ExportToR();
> |
> | where ExportToR is a method that creates an Rcpp::List from members of
> MyClass.
> | However, I'm unable to use components of Rcpp.h in my code; In
> particular, I'm
>
> If you want to use R in your own main, use RInside which makes this
> trivial. We ship it with several dozen examples.
>
> If you want to return a generic list structure, use the eg the STL type
> list
> (or, as you are using Blitz, maybe one of its containers).
>
> | unable to make this toy example work:
> |
> | #include <iostream>
> | #include <cstdlib>
> | #include <blitz/array.h>
> | #include <Rcpp.h>
> | #include <R.h>
> |
> | using namespace std;
> |
> | int main(int argc, char *argv[])
> | {
> | Rcpp::IntegerVector d;
> |
> | // Rcpp::List L; want to use
> | // L to output results to R.
> |
> | return EXIT_SUCCESS;
> | }
> |
> | I compile it with
> |
> | llvm-g++-4.2 -arch x86_64
> -I/Library/Frameworks/R.framework/Resources/include
> | -DNDEBUG -W -I/usr/local/include -I/usr/local/include
> -I"/Library/Frameworks/
> | R.framework/Versions/3.0/Resources/library/Rcpp/include"
> -L"/Library/Frameworks
> | /R.framework/Versions/3.0/Resources/library/Rcpp/lib/"
> -L"/Library/Frameworks/
> | R.framework/Versions/3.0/Resources/lib" export.cpp -lRcpp -lR -o export
> |
> | which compiles, but produces a segfault.
>
> To be expected. R is its own main program, you cannot mix as you did. I
> suspect you may find RInside useful; it deploys all of Rcpp for parameter
> exchange.
>
> Cheers. Dirk
>
>
> | Thanks for any help!
> |
> | Florian
> |
> |
> |
> |
> |
> |
> |
> | ----------------------------------------------------------------------
> | _______________________________________________
> | 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20131218/985f8a51/attachment-0001.html>
More information about the Rcpp-devel
mailing list