[Rcpp-devel] How to provide a custom generator/constructor for an Rcpp class?

Tom HABER tom.haber at uhasselt.be
Thu Mar 14 16:23:02 CET 2019


Hey,

I asked this question on stackoverflow (
https://stackoverflow.com/questions/54234934/how-to-provide-a-custom-generator-constructor-for-an-rcpp-class)
a while back but never got an answer, so I'm trying my luck here.

-----

I've written R bindings for my C++ library, but for usability I would like
to change the behavior of the methods and constructors somewhat in R:
adding defaults and packing variables into a list.

For example: instead of calling the generator ClassName(name, list(var1=1,
var2=2)), I would like to have ClassName(name, var1=1, var2=2)

Initially I was just using loadModules and using the C++ methods directly,
but then switched to setRcppClass which allows me to override the methods
including initialize. At first I tried using callNextMethod to no avail and
then basically resorted to copy-pasting what the Rcpp provided
initialize method
does with some small adjustments.

setRcppClass("ClassName", module="mymodule",
        methods=list(
            initialize=function(.Object, name, ...) {
                args <- list(...)
                argNames <- allNames(args)
                cppArgs <- nzchar(argNames)
                .CppObject <<- .CppGenerator$new(name, args[cppArgs])
            },
            show=function() {
                cat('ClassName of type:', .CppObject$name(), '\n')
            }))

This does almost what I want, except that I need to explicitly name the name
 parameter: ClassName(name="Foo", var1=1, var2=2)

Obviously I also want to avoid copying the Rcpp code, but instead call it
somehow.

Additionally R CMD check is complaining about all references to .CppGenerator
.CppObject allNames
-------

Thanks a lot in advance

Tom Haber
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20190314/561b5fa5/attachment-0001.html>


More information about the Rcpp-devel mailing list