0

I am attempting to change the value of a property, throughout the course of running an instantiated object. This is part of a download mechanism where value 20 is set to perform a first download of data for 20 lines, the rest of downloads will be set to 1 line. It is written in R object-oriented programming, using package R6.

The value is remembered because it is stored in the object (previously instantiated from class).

Script is being triggered from Rstudio (version: 1.1.456).

Observation:

The script do switch the property value from NULL to 20, in the first run.

Problem:

The value stays at 20 during all the script runs.

Wanted behaviour:

The first run, the value should start with NULL and swap to 20.

The second run of script, the property value should start being 20 thus react on the if-statement and change value to 1.

From 3rd run, and all rest of runs the value should star being 1 and the if-statement will not find any matching, keeping the value to 1.

# ----------------------------------------
# Classes.
# ----------------------------------------
Download <- R6Class("Download",
    public = list(
        # --------------------------------------------
        # Initializer:
        # --------------------------------------------
        initialize = function(value = NULL) {
            self$value <- value
        },

        # --------------------------------------------
        # Properties:
        # --------------------------------------------
        value = NULL,

        # --------------------------------------------
        # Functions:
        # --------------------------------------------
        run = function() {
            self$value_switcher()
        },
        value_switcher = function() {
            if(is.null(self$value)) {
                self$value = 20
            } else if(self$value == 20) {
                self$value = 1
            }
            cat("Your property value is :", self$value)
        }
    ) # Closure of list.
) # Closure of class.

download <- Download$new() # Instantiate the class.
download$run()             # Run object methods.
Toolbox
  • 2,333
  • 12
  • 26
  • I have never seen R code like this :P how does it know how many times the script has been run? can you give a bit of context as to why you are doing this? Maybe that will help us answer – morgan121 Jul 01 '19 at 06:48
  • The first `download$run()` sets the `value` to 20 and the second `download$run()` sets it to 1. That seems the intended behaviour. What's the problem? – nicola Jul 01 '19 at 07:00
  • $RAB Updated the question based on your comment. – Toolbox Jul 01 '19 at 07:08
  • @nicola Thanks for your comments. I realize now that I run the complete script several times, meanwhile running the download$run() several times actually does what expected (first run value = 20, rest of runs, value = 1). – Toolbox Jul 01 '19 at 07:16

0 Answers0