Hi, I am trying to create an R interface for Dynare++ [1], a k-order solver for rational expectation models. I would like some advice on how to glue the C++ code to R. In C++, it works the following way: - a DynareModelEq object is initialized with the formulas, parses them and performs k-order symbolic expansions - the user calls a member of this object multiple times with parameter values (eg for MCMC), the object returns tensors An R interface could work this way: modeleq <- dynare_expand(... model description ...) result1 <- dynare_substitute(modeleq, par1) result2 <- dynare_substitute(modeleq, par2) etc. So the user needs to see something in R that corresponds to a C++ object that remains in the memory between calls. What is the proper way to do that? Pass a pointer? How do I handle that in the .C() calls, convert it to long int? My C++ programming knowledge is pretty limited, suggestions and examples would be appreciated. The rest (using extern "C" constructs as described in "Writing R Extensions") is pretty clear, except how to make R call the destructor of the object when the garbage collector removes it. Thanks, Tamas [1] http://www.cepremap.cnrs.fr/juillard/mambo/index.php?option=com_content&task=view&id=53&Itemid=86
On Mon, Jul 24, 2006 at 03:00:30PM -0400, Dominick Samperi wrote:> R views the C++ code like a function call, so any persistence will have > to be > implemented through a database, or by passing working storage along with > the call. The data structures supported by RcppTemplate V4.2 may be helpful > for this purpose (in particular, data frames). This version of RcppTemplate > is still in the CRAN queue, so if you decide to take a look definitely wait > for this latest version (where complete data frame support is available).Dominick, I did look at RcppTemplate, but it seems that it only maps certain objects to R. The class I need to map is really complex and its elements have no obvious correspondence to R objects. Is there any way to return the pointer to a class from a .C call safely? Thanks, Tamas
On Mon, 24 Jul 2006, Tamas K Papp wrote:> Hi, > > I am trying to create an R interface for Dynare++ [1], a k-order > solver for rational expectation models. I would like some advice on > how to glue the C++ code to R. > > In C++, it works the following way: > > - a DynareModelEq object is initialized with the formulas, parses them > and performs k-order symbolic expansions > > - the user calls a member of this object multiple times with parameter > values (eg for MCMC), the object returns tensors > > An R interface could work this way: > > modeleq <- dynare_expand(... model description ...) > result1 <- dynare_substitute(modeleq, par1) > result2 <- dynare_substitute(modeleq, par2) > etc. > > So the user needs to see something in R that corresponds to a C++ > object that remains in the memory between calls. What is the proper > way to do that? Pass a pointer? How do I handle that in the .C() > calls, convert it to long int? My C++ programming knowledge is pretty > limited, suggestions and examples would be appreciated.R has an external pointer type for this purpose.> The rest (using extern "C" constructs as described in "Writing R > Extensions") is pretty clear, except how to make R call the destructor > of the object when the garbage collector removes it.You can register a C finalizer for an external pointer. This is a C function that will be called when the object is garbage collected, so you can call the destructor from there. AFAICT the only documentation for this is on the developer page, where there is a link to http://www.stat.uiowa.edu/~luke/R/simpleref.html I thought I remembered seeing something else, but I can't find anything now. -thomas Thomas Lumley Assoc. Professor, Biostatistics tlumley at u.washington.edu University of Washington, Seattle