[R] R6 object that is a list of referenced object

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Thu Nov 16 08:55:29 CET 2017


See below.

On Wed, 15 Nov 2017, Cristina Pascual wrote:

> Dear community,
>
> I am having a class, let's say Person,
>
> Person <-  R6Class("Person",
>                     public = list(
>                       idPerson = NULL,
>                       name = NULL,
>                       age = NULL,
>                       initialize = function(idPerson = NA, name = NA, age = NA) {

It is a bad idea to setup default values for all your parameters in any 
function, but particularly so for an initialization function. A Person 
with NA in the idPerson field is essentially unusable, so encouraging the 
creation of such an object is very bad practice.

>                        self$idPerson <- idPerson
>                        self$name <- name
>                        self$age <- age
>                       }
>                     ) # public
>
> ) # Person
>
> I have created:
> Person1 <- Person$new(1,'a',4)
> Person2 <- Person$new(2,'b',5)
>
> and I also have a class Community:
>
> Community <- R6Class("Community",
>                 public = list(
>                   e = NULL,
>                   initialize = function() self$e <- Person$new()

Initializing a Community with a bogus person is as bad as the idPerson 
being NA. It makes a lot more sense to have the set of persons in a 
community be the null set than to have a minimum of one person in the 
community who happens to have invalid identification.

>                 )
> )
>
> I want to create
>
> Community1 = List<Person>
>
> and add Person1 and Person2 to Community1 (Community1 <- Community1$add(Person1)
>                                                                                    Community1 <- Community1$add(Person2)
>
> ????)
>
> How can I write this with R6? I cannot find the proper example in the website.
>
> Can anybody help me?
>
> Thanks in advance,

You don't seem to be very familiar with either R or conventional 
object-oriented design. Although I am giving you a reprex below, I 
recommend that you avoid R6 until you are more familiar with how normal 
functional programming and S3 object oriented coding styles work in R. 
Using R6 as a crutch to avoid that learning process will only lead you to 
frustration and inefficient data handling. That is, this whole thing 
should just be a data frame.

########################################
library(R6)
Person <-  R6Class( "Person"
                   , public = list( 
idPerson = NA
                                  , 
name = NA
                                  , 
age = NA
                                  , 
initialize = function( idPerson

                      , name

                      , age

                      ) {

                self$idPerson <- 
idPerson

                self$name <- name

                self$age <- age

              }
                      ) # public
                   ) # Person

Person1 <- Person$new( 1, 'a', 4 )
Person2 <- Person$new( 2, 'b', 5 )

Community <- R6Class( "Community"
                     , public = 
list( e = NULL

, addPerson = function( p ) {

    self$e <- append( self$e, p )

   }

)
                     )

Community1 <- Community$new()
Community1$addPerson( Person1 )
Community1$addPerson( Person2 )
Community1$e
#> [[1]]
#> <Person>
#>   Public:
#>     age: 4
#>     clone: function (deep = 
FALSE)
#>     idPerson: 1
#>     initialize: function 
(idPerson, name, age)
#>     name: a
#>
#> [[2]]
#> <Person>
#>   Public:
#>     age: 5
#>     clone: function (deep = 
FALSE)
#>     idPerson: 2
#>     initialize: function 
(idPerson, name, age)
#>     name: b

# Standard R approach:
Person1a <- data.frame( idPerson = 1
                       , name = "a"
                       , age = 4
                       , stringsAsFactors = FALSE
                       )
Person2a <- data.frame( idPerson = 2
                       , name = "b"
                       , age = 5
                       , stringsAsFactors = FALSE
                       )
Community1a <- rbind( Person1a, Person2a )
Community1a
#>   idPerson name age
#> 1        1    a   4
#> 2        2    b   5
########################################

>
>
>
> 	[[alternative HTML version deleted]]

Please POST IN PLAIN TEXT FORMAT. This is a setting you must make in 
your email program, and failure to do so will lead to us seeing different 
things than you send (that is, we see varying degrees of scrambling of 
your message if you send HTML-formatted emails). Read the Posting Guide 
mentioned below for more success tips.

>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k



More information about the R-help mailing list