Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transforming an object from the old "reference class" or S4 verfsion of object to R6: #57

Open
rburghol opened this issue May 26, 2021 · 0 comments

Comments

@rburghol
Copy link
Contributor

rburghol commented May 26, 2021

  • Base class declaration function: Change SetRefClass(... to R6Class(
  • Syntax for inheritance: Change: contains = "openmi.om.linkableComponent", to inherit = openmi.om.linkableComponent, (notice no quot3es around class name -- very important)
  • referencing the object local fields. In R6, need to use the self$ prefix before class attributes, so data changes to self$data
  • Referencing super class methods:
    • change "callSuper()" to "super$methodname()"
  • The "<<-" for assignment of self attributes goes to "<-"

S4 version:

#****************************
# Override logState() method and has write method on finish()
#****************************
#' The base class for logging component data.
#'
#' @param
#' @return reference class of type openmi.om.linkableComponent
#' @seealso
#' @export openmi.om.logger
#' @examples
openmi.om.logger <- setRefClass(
  "openmi.om.logger",
  fields = list(
    directory = "character",
    path = "character",
    filename = "character",
    outputs = "ANY"
  ),
  contains = "openmi.om.linkableComponent",
  # Define the logState method in the methods list
  methods = list(
    update = function () {
      callSuper()
    },
    initialize = function () {
      callSuper()
    },
    logState = function () {
      callSuper()
      if (!is.xts(outputs)) {
        # first time, we need to initialize our data columns which includes timestamp
        # @todo: be more parsimonius?
        outputs <<- xts(data.frame(data), order.by = as.POSIXct(data$thistime))
      } else {
        outputs <<- rbind(outputs, xts(data.frame(data), order.by = as.POSIXct(data$thistime)))
      }
    }
  )
)

R6 version

#****************************
# Override logState() method and has write method on finish()
#****************************
#' The base class for logging component data.
#'
#' @param
#' @return reference class of type openmi.om.linkableComponent
#' @seealso
#' @export openmi.om.logger
#' @examples
openmi.om.logger <- RClass(
  "openmi.om.logger",
  inherit = openmi.om.linkableComponent,
  public = list(
    directory = "character",
    path = "character",
    filename = "character",
    outputs = "ANY"
  ),
  inherit = openmi.om.linkableComponent,
  # Define the logState method in the methods list
  methods = list(
    update = function () {
      callSuper()
    },
    initialize = function () {
      callSuper()
    },
    logState = function () {
      callSuper()
      if (!is.xts(outputs)) {
        # first time, we need to initialize our data columns which includes timestamp
        # @todo: be more parsimonius?
        outputs <<- xts(data.frame(data), order.by = as.POSIXct(data$thistime))
      } else {
        outputs <<- rbind(outputs, xts(data.frame(data), order.by = as.POSIXct(data$thistime)))
      }
    }
  )
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant