add_handler: Add Handler

Description Usage Arguments Value Examples

View source: R/add_handler.R

Description

Merge two handler objects or add a handler in a handlers_list or dispatcher objects.

Usage

1
add_handler(obj, .handler)

Arguments

obj

A handler, handler_list or dispatcher object.

.handler

A handler object.

Value

Retun a handlers_list object.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
library(eventr)
library(magrittr)

birth_handler <- handler(type = 'BIRTH', FUN = function(obj, event){
  obj$birth_date <- get_body_attr(event, 'birth_date')
  return(obj)
})

measurement_handler <- handler(type = 'MEASUREMENT', FUN = function(obj, event){

  obj$weight <- get_body_attr(event, 'weight')
  obj$height <- get_body_attr(event, 'height')
  return(obj)

})

death_handler <- handler(type = 'DEATH', FUN = function(obj, event){
  obj$death_date <- get_body_attr(event, 'death_date')
  return(obj)
})

handlers <- add_handler(birth_handler, death_handler)

handlers_01 <- birth_handler %>%
  add_handler(measurement_handler) %>%
  add_handler(death_handler)

handlers_02 <- birth_handler +
  measurement_handler +
  death_handler

identical(handlers_01, handlers_02)

eventr documentation built on July 8, 2020, 7:32 p.m.