In large projects I sometime want to have a standardized and rigid data 'object' such that any function of that data may confidently assume numerous properties of the object with no more than an assertion that the object is of the expected class. So I'm very happy to have discovered R6 classes, which seem to allow this by providing 'private' elements as follows:
library('R6')
Data = R6::R6Class("Data",
private = list(x = NA, y = pi),
public = list(
initialize = function(x, y) {
private$x = x
},
get = function(attribute) return(private[[attribute]])
)
)
data = Data$new(x = 5)
data$get('x')
data$get('y')
This get
function is a hack. What I really want is for the attributes of data
to be accessible simply as data$x
or data[['x']]
while still having the tamper-proof qualities of private variables. Is there a better way to achieve this?