Session Ready
Exercise

Attack of the Clones (2)

If an R6 object contains another R6 object in one or more of its fields, then by default clone() will copy the R6 fields by reference. To copy those R6 fields by value, the clone() method must be called with the argument deep = TRUE.

a_deep_copy <- an_r6_object$clone(deep = TRUE)
Instructions
100 XP

A PowerPlug R6 class to describe the microwave's power plug, has been predefined in your workspace. The MicrowaveOven class has been updated to include a PowerPlug object.

  • Create a MicrowaveOven object and assign it to a_microwave_oven.
  • Copy the microwave into a variable named cloned_microwave_oven using the clone() method, with no arguments.
  • Copy the microwave into a variable named deep_cloned_microwave_oven using the clone() method, passing deep = TRUE.
  • Change the type field of the power_plug field of a_microwave_oven to "British".
  • Verify that the power_plug$type fields of a_microwave_oven and cloned_microwave_oven are identical.
  • Verify that the power_plug$type fields of a_microwave_oven and deep_cloned_microwave_oven are different.