[Rcpp-devel] Expose cpp class with built-in boost odeint to R
Simon Woodward
Simon.Woodward at dairynz.co.nz
Sat Sep 15 00:05:42 CEST 2018
Hi Ralf
Thank you so much, your advice was extremely helpful! It works now.
My simulation model will be 1000+ lines of C++, this is a prototype, I embedded the code inline to make it easier to post as a reprex to this forum.
Follow up questions:
- do I need to worry about destructing my global variable my_spring (the instance of my class)? If so, how do I do this?
- for read/write efficiency, can/should I make a persistent link (Rccp::XPtr?) between the my_spring.variable property and an object or function in R? If so, how do I do this? Or is it better simply to copy-pass the data when needed and let Rccp handle the conversion from std::unordered_map<string,double> to an R named vector?
// https://www.r-bloggers.com/external-pointers-with-rcpp/
Thanks!
Simon
-----Original Message-----
From: Rcpp-devel [mailto:rcpp-devel-bounces at lists.r-forge.r-project.org] On Behalf Of Ralf Stubner
Sent: Friday, 14 September 2018 8:05 PM
To: rcpp-devel at lists.r-forge.r-project.org
Subject: Re: [Rcpp-devel] Expose cpp class with built-in boost odeint to R
On 14.09.2018 05:17, Simon Woodward wrote:
> I am developing a simulation model in C++ and I want to expose it to R for testing. The model is in a C++ class and uses the boost::odeint library for numerical integration. I am also using an unordered_map to expose the model variables to the user.
> I am having some trouble exposing the class functionality to C++, either as separate // [[Rcpp::export]] methods or using RCPP_MODULE. The following code successfully instantiates the model and returns the unordered_map but crashes Rcpp if I try to access the advance(t,dt) method.
What do you mean with "crashes Rcpp"? On my system it mearly gives a compiler error:
/usr/include/c++/6/bits/stl_iterator_base_funcs.h: In instantiation of 'void std::advance(_InputIterator&, _Distance) [with _InputIterator = Rcpp::InputParameter<double>; _Distance = Rcpp::InputParameter<double>]':
> # sample code embedded in an R script
With the given ratio of C++ to R code it would have been better to use a
C++ file with R code embedded via /*** R ... */.
> library(Rcpp)
>
> Sys.setenv("PKG_CXXFLAGS"='-I"C:/boost/boost_1_66_0"')
The BH package can help with that:
// [[Rcpp::depends(BH)]]
> sourceCpp(code='
> #include <unordered_map>
> #include <string>
> #define BOOST_CONFIG_SUPPRESS_OUTDATED_MESSAGE
> #include <boost/numeric/odeint.hpp>
> #include <Rcpp.h>
>
> // Enable C++11 via this plugin (Rcpp 0.10.3 or later)
> // [[Rcpp::plugins(cpp11)]]
>
> using namespace std;
The above quoted error message indicates that your advance method and std::advance are getting mixed up. Remove this "using namespace std;"
and handle to resulting errors by using "std::" or "using std::...;".
BTW, for larger pieces of code I would also get rid of "using namespace Rcpp;".
cheerio
ralf
--
Ralf Stubner
Senior Software Engineer / Trainer
daqana GmbH
Dortustraße 48
14467 Potsdam
T: +49 331 23 61 93 11
F: +49 331 23 61 93 90
M: +49 162 20 91 196
Mail: ralf.stubner at daqana.com
Sitz: Potsdam
Register: AG Potsdam HRB 27966 P
Ust.-IdNr.: DE300072622
Geschäftsführer: Prof. Dr. Dr. Karl-Kuno Kunze
More information about the Rcpp-devel
mailing list