Skip to content

The ts_tree_select<-() replacement function works similarly to the combination of ts_tree_select() and ts_tree_update(), but it might be more readable.

Usage

ts_tree_select(tree, ...) <- value

Arguments

tree

A ts_tree object as returned by ts_tree_new().

...

Selection expressions, see ts_tree_select().

value

An R expression to serialize or ts_tree_deleted().

Value

A ts_tree object with the selected parts updated.

ts_tree_deleted() returns a marker object to be used at the right hand side of the ts_tree_select<- or the double bracket replacement functions, see examples below.

Details

The following two expressions are equivalent:

 

tree <- ts_tree_select(tree, <selectors>) |> ts_tree_update(value)
and

 

ts_tree_select(tree, <selectors>) <- value

 

json <- tsjsonc::ts_parse_jsonc(
  '{ "a": 1, "b": [10, 20, 30], "c": { "c1": true, "c2": null } }'
)
json
#> # jsonc (1 line)
#> 1 | { "a": 1, "b": [10, 20, 30], "c": { "c1": true, "c2": null } }

 

json |> ts_tree_select("b", 1)
#> # jsonc (1 line, 1 selected element)
#> > 1 | { "a": 1, "b": [10, 20, 30], "c": { "c1": true, "c2": null } }

 

ts_tree_select(json, "b", 1) <- 100
json
#> # jsonc (1 line)
#> 1 | { "a": 1, "b": [100, 20, 30], "c": { "c1": true, "c2": null } }

ts_tree_deleted() is a special marker to delete elements from a ts_tree object with ts_tree_select<- or the double bracket operator.

Examples

# Create a parse tree with tsjsonc -------------------------------------
tree <- tsjsonc::ts_parse_jsonc('{"a": 13, "b": [1, 2, 3], "c": "x"}')
tree
#> # jsonc (1 line)
#> 1 | {"a": 13, "b": [1, 2, 3], "c": "x"}

ts_tree_select(tree, "a") <- 42
ts_tree_select(tree, "b", -1) <- ts_tree_deleted()

tree
#> # jsonc (1 line)
#> 1 | {"a": 42, "b": [1, 2], "c": "x"}