Learn R Programming

commandr (version 1.0.1)

Operation-class: Operation: A Simple Command Implementation

Description

An Operation object implements a Command with an R closure. This may be useful as a base for customized Command derivatives. Otherwise, consider it a skeletal proof-of-concept.

Arguments

Constructors

Operation(do, undo = NULL): Constructs an Operation that invokes the closure do. If undo is specified, it is the closure invoked for the reverse operation.
OperationQ(do, undo): Constructs an Operation that invokes a function with the body taken from the quoted argument do. The function is enclosed in the calling environment. If undo is specified, it is treated equivalently to do, except it should implement the reverse operation.

Methods

eval(expr): Executes the operation by evaluating the closure.
rev(x): Returns a new Operation that performs the reverse operation, as long as undo was provided.

Examples

Run this code
  op <- OperationQ(message("hello world"))
  eval(op)

  x <- 2
  op <- OperationQ(x^2, sqrt(x))
  x <- eval(op)
  rop <- rev(op)
  identical(2, eval(rop))

Run the code above in your browser using DataLab