Assuming that the class and instantiation of class are held in 2 separate files, how would you import the class data prior to instantiate the class?
Below code works fine if held in one same file, but I suspect that as soon as the code base starts growing you would want to split the data into smaller code chunks.
Should I use [source], does it exist an autoloader or any another guideline?
File: _class_data.R
if (!"package:R6" %in% search()) {
library(R6)
}
# Class 1
Class_1 <- R6Class("Class_1",
public = list(
# Properties:
x = 0,
# Lists:
credentials = list(
user = "user",
password = "pass"
),
# Functions:
myFunction = function() {
return(self$x)
}
)
)
File: run.R
# Should I add a [source] path here to [ _class_data.R] ?
# Instantiate a class by creating an object.
class_1 <- Class_1$new()
The most straightforward way is to first run or source the class file. In this case the result will be an environment object that is stored in R:s global environment. This is the class.
As a second step you create an object, by instantiate the same class. If this instantation is kept within a separate file, you would have to run or source that file also.
Since both objects (class and object) will now exist in the Global environment you can now decide if you want to remove the class and just keep the object.
Following standard guidelines the only name convention difference between the 2 objects would be that the class name start with a capital letter, meanwhile the object holds same name but with all characters in lower case.
If the amount of classes grows it is of course unpractical to administrate the objects one-by-one, and you would probably need some autoload logics.