createCrudExpression: Create CRUD expression

Description Usage Arguments Details Value Author(s) References See Also Examples

View source: R/crud.R

Description

Creates an CRUD expression. This either is an expression for value extraction involving the syntactic sugars for extraction ([[ or $; see Extract) or one that also involves the syntactic sugar for assignments (<-; see assignOps)

Usage

1

Arguments

inst

Object for method dispatch.

...

Further arguments to be passed to subsequent functions/methods.

Details

Known methods:

For a faster but less comprehensive version see: createCrudExpressionPlain

Value

See respective methods.

Author(s)

Janko Thyson janko.thyson@rappster.de

References

http://github.com/rappster/crudr

See Also

Extract, assignOps

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Example object ----------------------------------------------------------

inst <- list(a = list(b = list(c = 10)))

# Extract -----------------------------------------------------------------

createCrudExpression(inst, c("a", "b"))

createCrudExpression(inst, c("a", "b"), evaluate = TRUE)
## --> evaluated inside the function

createCrudExpression(inst, c("a", "b"), evaluate = TRUE, in_parent = TRUE)
## --> evaluated in the frame of `inst`

# Assign ------------------------------------------------------------------

createCrudExpression(inst, c("a", "b", "c"), 100)
createCrudExpression(inst, c("a", "b", "c"), 100, evaluate = TRUE)
## --> evaluated inside the function
inst
## --> `inst$a$b$c` still is `10`

createCrudExpression(inst, c("a", "b", "c"), 100, evaluate = TRUE,
  in_parent = TRUE)
## --> evaluated in the frame of `inst`
inst
## --> `inst$a$b$c` is now `100`

# Return values -----------------------------------------------------------

inst <- list(a = list(b = list(c = 10)))

createCrudExpression(inst, c("a", "b", "c"), 100, evaluate = TRUE,
  return_conventional = FALSE)
## --> evaluated inside the function and entire "updated" object `inst`returned
inst
## --> `inst$a$b$c` still is `10`

createCrudExpression(inst, c("a", "b", "c"), 1, evaluate = TRUE,
  in_parent = TRUE, return_conventional = FALSE)
## --> evaluated in the frame of `inst`. In this case, `NULL` is returned
##     in order to stress the point that the actual object being altered
##     has nothing to do with the return value
inst
## --> `inst$a$b$c` is now `1`

# Sugar = $ ---------------------------------------------------------------

## For the sake of completeness the function also allows you to use the
## syntactic sugar `$` instead of `[[`. While this might be usefull in
## certain usage scenarios, it needs to be noted that comes with additional
## computiational costs (see benchmark)

inst <- list(a = list(b = list(c = 10)))
createCrudExpression(inst, c("a", "b"), sugar = "$")
createCrudExpression(inst, c("a", "b"), value = 1, sugar = "$")

library(microbenchmark)
res <- microbenchmark(
  "extract_1" = createCrudExpression(inst, c("a", "b"), sugar = "[["),
  "extract_2" = createCrudExpression(inst, c("a", "b"), sugar = "$"),
  "assign_1" = createCrudExpression(inst, c("a", "b"), 1, sugar = "[["),
  "assign_2" = createCrudExpression(inst, c("a", "b"), 1, sugar = "$")
)
res

# Comparison to plain method ----------------------------------------------

## While `createCrudExpression` offers more features,
## `createCrudExpressionPlain` is substantially faster.

library(microbenchmark)
res <- microbenchmark(
  "extract_1" = createCrudExpression(inst, c("a", "b")),
  "extract_2" = createCrudExpressionPlain(inst, c("a", "b")),
  "assign_1" = createCrudExpression(inst, c("a", "b"), 1),
  "assign_2" = createCrudExpressionPlain(inst, c("a", "b"), 1)
)
res


# Bridging ----------------------------------------------------------------

inst <- list(a = list(b = 1))
createCrudExpression(inst, c("a", "b", "c", "d"), value = 1, evaluate = TRUE,
  return_conventional = FALSE)
createCrudExpression(inst, c("a", "b", "c", "d"), value = 1, evaluate = TRUE,
  return_conventional = FALSE, strict = 1)
try(
  createCrudExpression(inst, c("a", "b", "c", "d"), value = 1, evaluate = TRUE,
  return_conventional = FALSE, strict = 2)
)
try(
  createCrudExpression(inst, c("a", "b", "c", "d"), value = 1, evaluate = TRUE,
  return_conventional = FALSE, strict = 3)
)

rappster/crudr documentation built on May 26, 2019, 11:12 p.m.