r-lib / R6

Encapsulated object-oriented programming for R
https://R6.r-lib.org
Other
405 stars 56 forks source link

Creating List of R6 Objects Fails #89

Closed DarioS closed 8 years ago

DarioS commented 8 years ago

Unlike S4 objects, assigning them to a list with <<- fails. With R6,

> class(aPerson)
[1] "Person" "R6"
> personList <<- list(aPerson)
Error: cannot change value of locked binding for 'personList'

With S4,

track <- setClass("track", slots = c(x="numeric", y="numeric"))
t1 <- track(x = 1:10, y = 1:10 + rnorm(10))
trackList <<- list(t1)

A workaround for R6 without errors is

assign("personList", list(aPerson), envir = .GlobalEnv)

but the <<- way should be fixed. This makes it harder to assign or append Observer objects created by observeEvent to a list defined in global.R of a Shiny application.

wch commented 8 years ago

Please provide a reproducible example.

On Monday, June 27, 2016, DarioS notifications@github.com wrote:

Unlike S4 objects, assigning them to a list with <<- fails. With R6,

class(aPerson) [1] "Person" "R6" personList <<- list(aPerson) Error: cannot change value of locked binding for 'personList'

With S4,

track <- setClass("track", slots = c(x="numeric", y="numeric")) t1 <- track(x = 1:10, y = 1:10 + rnorm(10)) trackList <<- list(t1)

A workaround for R6 without errors is

assign("personList", list(aPerson), envir = .GlobalEnv)

but the <<- way should be fixed. This makes it harder to assign or append Observer objects created by observeEvent to a list defined in global.R of a Shiny application.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/wch/R6/issues/89, or mute the thread https://github.com/notifications/unsubscribe/AAFTwnP503KeFTdMulO9Jou1EcTDSgnhks5qP3UmgaJpZM4I-zwX .

Sent from my phone

DarioS commented 8 years ago

It is based on the introductory vignette's example. All of the code is:

library(R6)
Person <- R6Class("Person",
  public = list(
    name = NA,
    hair = NA,
    initialize = function(name, hair) {
      if (!missing(name)) self$name <- name
      if (!missing(hair)) self$hair <- hair
    }))

aPerson <- Person[["new"]]("Adam", "Black")    
personList <<- list(aPerson)  # Error produced.
wch commented 8 years ago

The problem is that personList is the name of an object in the utils package and so the <<- is trying to assign a new value to that binding, which is locked. If you use a different name, like personList1, it works fine.

On Monday, June 27, 2016, DarioS notifications@github.com wrote:

It is based on the introductory vignette's example. All of the code is:

library(R6) Person <- R6Class("Person", public = list( name = NA, hair = NA, initialize = function(name, hair) { if (!missing(name)) self$name <- name if (!missing(hair)) self$hair <- hair }))

aPerson <- Person["new"] personList <<- list(aPerson) # Error produced.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/wch/R6/issues/89#issuecomment-228920077, or mute the thread https://github.com/notifications/unsubscribe/AAFTwnZyoLFHsPSbaqGovaT1NhxOCizeks5qQHIYgaJpZM4I-zwX .

Sent from my phone

DarioS commented 8 years ago

Oh, amazing variable-naming coincidence!

y5mei commented 1 year ago

Maybe this is a off-topic question, but why I cannot get access to the name field of the personList1 elements? Say, in the example below, I was trying to get Tom to be printed, but I got NULL

Person <- R6Class("Person",
public = list(
name = NA,
hair = NA,
initialize = function(name, hair) {
if (!missing(name)) self$name <- name
if (!missing(hair)) self$hair <- hair
}))

aPerson <- Person$new("Tom")
class(aPerson)
personList1 <<- list(aPerson)
print(personList1[1]$name) # NULL is printed instead of Tom???
wch commented 1 year ago

@y5mei Instead of:

personList1[1]$name

You need to use:

personList1[[1]]$name