Simple object in R Project

Let’s create an “Account” class using R language RC object with two methods: displayBalance and add.

Account <- setRefClass("Account",
  fields = list(name = "character", total = "numeric"),
  methods = list(
    displayBalance = function() {
      paste0("The account of", name, "is ", total, "$")
    },
    add = function(x) {
      total <<- total + x
    }
  )
)



Create an RC object, access it variable and call it methodes

myAccount <- Account$new(name="Martin Magakian", total=100)
myAccount$add(100)
balance <- myAccount$displayBalance()
print(balance)

will print on the screen:
The account of Martin Magakian is 200$