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, ...) <- valueArguments
- tree
A
ts_treeobject as returned byts_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)
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.
See also
Other ts_tree generics:
[[.ts_tree(),
[[<-.ts_tree(),
format.ts_tree(),
print.ts_tree(),
ts_tree_ast(),
ts_tree_delete(),
ts_tree_dom(),
ts_tree_format(),
ts_tree_insert(),
ts_tree_new(),
ts_tree_query(),
ts_tree_select(),
ts_tree_sexpr(),
ts_tree_unserialize(),
ts_tree_update(),
ts_tree_write()
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"}