НачатьНачать бесплатно

Specifying the Microwave Oven Class

To create R6 objects, you first have to create a class generator, sometimes known as a factory. These are created using the R6Class() (docs) function.

The first argument to R6Class() is the name of the class of the objects that are created. By convention, this is written in UpperCamelCase. Another argument to R6Class() is called private and holds the data fields for the object. This argument should be a list, with names for each of its elements.

Further arguments to R6Class() will be discussed in the coming exercises. The pattern for defining an object factory is as follows.

thing_factory <- R6Class(
  "Thing",
  private = list(
    a_field = "a value",
    another_field = 123
  )
)

Это упражнение является частью курса

Object-Oriented Programming with S3 and R6 in R

Посмотреть курс

Инструкции к упражнению

  • Define a factory for a microwave oven.
    • Call R6Class.
    • The class name should be "MicrowaveOven".
    • The private element should be a list.
    • That list should contain a single field, named power_rating_watts, taking the value 800.

Интерактивное практическое упражнение

Попробуйте выполнить это упражнение, дополнив этот пример кода.

# Define microwave_oven_factory
microwave_oven_factory <- ___(
  "___",
  ___ = ___(
    ___ = ___
  )
)
Редактировать и запускать код