Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201945

Automatically remove R6 object

$
0
0

I have created a bank account class with the R6 package . It has a balance that is private (not accessible after generation) and methods to withdraw and deposit a certain amount as well as a print method. (Hadley Wickham 14.3.3)

BankAccount <- R6::R6Class("BankAccount", public = list(
  # Functions to withdraw and deposit.
  deposit = function(amount){
    private$balance <- private$balance + amount
  },
  withdraw = function(amount){
    if (amount < private$balance){
    private$balance <- private$balance - amount
    }
    else {
      stop("Amount withdrawn is greater than available balance by", -(private$balance - amount))
    }
  },
  initialize = function(initial_balance){
    private$balance <- initial_balance
  },
  print = function(){
    cat("Bank balance is equal to", private$balance)
  }
  ),
  private = list(
    balance = NULL
  )
)

Currently, if I create a new R6 object and call the withdraw function with an amount greater than its initial balance, the function will stop and print a message. Instead, I would like the newly created object to be directly removed. How can I achieve this?


Viewing all articles
Browse latest Browse all 201945

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>