2

I was wondering if it's possible to get the object constructor from within an R6 object. One obvious way would be something like this:

test <- R6::R6Class('test',
                        public = list(initialize = function(myConst = test) private$myConst <- myConst),
                    private = list(myConst = NULL),
                    active = list(getConst = function()private$myConst))

but this looks to me really unsafe; for example, producing a new class that inherits from this but does not redefine the initialisation would clearly brake the game.

The reason for asking this is that I would like to include a method that saves the object to an HDF5 file, containing a section with the data (accessible to other uses or languages), and the serialised constructor as well, making it possible to reconstruct the object from the file.

5
  • Why not just save the name of the class and the name of the package that defines it? Then a reconstructor could build it. Commented Feb 5 at 9:25
  • @user2554330, yes that would be the easiest way... I was thinking to a more general approach where the superclass (defined in its own package) has this save method, and I can create a new class (in my volatile working env) with its own data structure that inherits the save method from the superclass. Commented Feb 5 at 10:49
  • 1
    If you are inheriting from the superclass, then the code in the superclass must be available to you, so there's no need to save a copy of part of it (and worry about whether it is obsolete, etc.) Commented Feb 5 at 13:52
  • When you say constructor, you're talking about the R6::R6Class(..) call, right? Commented Feb 5 at 16:27
  • 2
    @r2evans, mmmh... no, I mean (in my example) the object "test" with class "R6ClassGenerator" returned by that call... In the end I suppose it is just a matter of including a wrapper function that do function(x)x$new(myConst = x)... not very elegant though Commented Feb 5 at 16:58

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.