The identical() function seems to give the correct answer, but the documentation doesn't explicitly discuss object references. The closest note in the documentation is:
Checking equality for two large, complicated objects can take longer if the objects are identical or nearly so, but represent completely independent copies.
Some examples of using identical():
QuickClass <- R6::R6Class("QuickClass",
public = list(
initialize = function(x) {
private$px <- x
}
),
active = list(
x = function(px) {
if(missing(px)) return(private$px)
else private$px <- px
}
),
private = list(
px = 0
)
)
> a <- QuickClass$new(1)
> identical(a, a)
[1] TRUE
> b <- a
> identical(a, b)
[1] TRUE
> c <- QuickClass$new(2)
> identical(a, c)
[1] FALSE
> d <- QuickClass$new(1)
> identical(a, d)
[1] FALSE
So, identical looks to do what is needed, I just want to check if there is a better way e.g. a specific function that just compares object references so may be faster and more directly applicable. identical() looks like it can resort to field-by-field comparisons.
Contrast clause: This question is similar to In R, how can I check if two variable names reference the same underlying object? - however that question is quite old (pre-R6 classes) and the answers discuss using low-level techniques that I would rather avoid.