How to provide a custom generator/constructor for an Rcpp class?

Viewed 105

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

0 Answers
Related