`new_box()`

is similar to `base::I()`

but it protects a value by
wrapping it in a scalar list rather than by adding an attribute.
`unbox()`

retrieves the boxed value. `is_box()`

tests whether an
object is boxed with optional class. `as_box()`

ensures that a
value is wrapped in a box. `as_box_if()`

does the same but only if
the value matches a predicate.

new_box(.x, class = NULL, ...)
is_box(x, class = NULL)
unbox(box)

## Arguments

class |
For `new_box()` , an additional class for the
boxed value (in addition to `rlang_box` ). For `is_box()` , a class
or vector of classes passed to `inherits_all()` . |

... |
Additional attributes passed to `base::structure()` . |

x, .x |
An R object. |

box |
A boxed value to unbox. |

## Examples

boxed <- new_box(letters, "mybox")
is_box(boxed)

#> [1] TRUE

is_box(boxed, "mybox")

#> [1] TRUE

is_box(boxed, "otherbox")

#> [1] FALSE

unbox(boxed)

#> [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
#> [20] "t" "u" "v" "w" "x" "y" "z"

#> [[1]]
#> [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
#> [20] "t" "u" "v" "w" "x" "y" "z"
#>
#> attr(,"class")
#> [1] "mybox" "rlang_box"

unbox(boxed2)

#> [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
#> [20] "t" "u" "v" "w" "x" "y" "z"

# Compare to:
boxed_boxed <- new_box(boxed, "mybox")
boxed_boxed

#> [[1]]
#> [[1]]
#> [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
#> [20] "t" "u" "v" "w" "x" "y" "z"
#>
#> attr(,"class")
#> [1] "mybox" "rlang_box"
#>
#> attr(,"class")
#> [1] "mybox" "rlang_box"

unbox(unbox(boxed_boxed))

#> [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
#> [20] "t" "u" "v" "w" "x" "y" "z"

#> [[1]]
#> NULL
#>
#> attr(,"class")
#> [1] "null_box" "rlang_box"

#> [1] "foo"