[Rd] Create and access several instances of a C++ class from R

soeren.vogel at uzh.ch soeren.vogel at uzh.ch
Fri May 6 19:24:10 CEST 2011


We have a C++ class with several methods that manipulate an object. How is it possible to create several instances of that class *from R* in the C++ realm, which can then be accessed via a given name character? Symbolic example (we hope this illustrates our problem):

// C++ side:
class Foo{
// perhaps:
void my_new_instance_wrapper("the_character") // plain to see that I am no C++ programmer ;-)
  static Foo "the_character"; // no return needed since we know the name of the instance = "the_character"

# R side:
create_new_instance <- function(name){
  dono_what_to_use_here(???, class)
# perhaps:
create_new_instance <- function(name){
  .C("my_new_instance_wrapper", as.character(name))

obj1 <- create_new_instance("bar", class="Foo")
obj2 <- create_new_instance("baz", class="Foo")
: character which can be manipulated using class methods in the C++ realm

What we do not want: make simple copies of the object in R; use Rcpp modules (we tried that without success, pointers in constructors cause trouble); re-write our code such that C++ only "works off" heavy code, the rest is R-side. What we want: interfacing (from the R-side) instances of our class where the instances exist in the C++ realm.

Either there is a function (or code) in R that solve this task, perhaps by returning pointers to instances of C++ classes. (Or there is a possibility to create a wrapper in C++ creating a new instance, the wrapper we do not know of.)

Thanks for any notes, tips, experiences.

Sören and Carlo

More information about the R-devel mailing list