This recipe explains what about initialize() function in R
When we declare an object in R, the object can be classified either as public data element or private data element. Initialise is a function used to initialise a variable in the form of private data element ​
This recipe demonstrates how to initilise any values to the objects at the time of declaration of the objects. ​
We do that by inclusisng public and private data members ​
library(R6)
football <- R6Class(
"Football",
private = list(
name_of_player = NA,
goals_scored = NA),
public = list(
initialize = function(x,y){
private$name_of_player <-x
private$goals_scored <-y
})
)
The above code means that we trying to initialise the values of "name_of_player" and "goals_scored" during the time of execution/declaration ​
man_utd <- football$new("Paul Pogba", 11)
man_utd
Public: clone: function (deep = FALSE) initialize: function (x, y) Private: goals_scored: 11 name_of_player: Paul Pogba
This means that we have succeeded in initialzing the values "Paul Pogba" to name_of_player and "11" to goals_scored. ​