The [[ operator works similarly to the combination of
ts_tree_select() and
ts_tree_unserialize(), but it
might be more readable.
Arguments
- x
A
ts_treeobject.- i
Selection expressions in a list, see details in
ts_tree_select().- ...
Additional arguments, passed to
ts_tree_select().
Details
The following two expressions are equivalent:
ts_tree_select(tree, <selectors>) |> ts_tree_unserialize()
tree[[list(<selectors>)]]
json <- tsjsonc::ts_parse_jsonc(
'{ "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 } }
json[[list("b", 1)]]#> [[1]] #> [1] 10 #>
The [[<- replacement operator
The [[<- operator works similarly to the combination of
ts_tree_select() and
ts_tree_update(), (and also to the
replacement function ts_tree_select<-()),
but it might be more readable.
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 } }
json[[list("b", 1)]] <- 100
json#> # jsonc (1 line) #> 1 | { "a": 1, "b": [100, 20, 30], "c": { "c1": true, "c2": null } }
See also
Other ts_tree generics:
[[<-.ts_tree(),
format.ts_tree(),
print.ts_tree(),
select-set,
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()
Other serialization functions:
ts_tree_unserialize()
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"}
tree[[list("a")]]
#> [[1]]
#> [1] 13
#>
# Last two elements of "b"
tree[[list("b", -(1:2))]]
#> [[1]]
#> [1] 2
#>
#> [[2]]
#> [1] 3
#>