treeToJSON: Converts a data.tree to a JSON format

View source: R/data.tree-conversion.R

treeToJSONR Documentation

Converts a data.tree to a JSON format

Description

Walk through a data.tree and constructs a JSON string, which can be rendered by shinyTree.

Usage

treeToJSON(
  tree,
  keepRoot = FALSE,
  topLevelSlots = c("default", "all"),
  createNewId = TRUE,
  pretty = FALSE
)

Arguments

tree

the data.tree which should be parses

keepRoot

logical. If FALSE (default) the root node from the tree is pruned

topLevelSlots

determines which slots should be moved to the top level of the node. If default or NULL slots used in the jsTree JSON are kept on the top level, while any other atomic / list slots from the tree are stored in an own slot called ‘data’. If all *all* nodes are stored on the top level. Alternatively, it can be an explicit vector of slot names which should be kept. In the latter case it is the user's responsibility to ensure that jsTree slots stay on the top level.

createNewId

logical. If TRUE a new id will be generated. Any old ‘id’ will be stored in ‘id.orig’ and a warning will be issued, If FALSE, any existing id will be re-used.

pretty

logical. If TRUE the resulting JSON is prettified

Details

The JSON string generated follows the jsTree specifications. In particular it encodes children nodes via the ‘children’ slot.

All atomic or list slots of a node in the tree are stored in a data slot in the resulting JSON.

If the user wants to store some slots not in the data slot but on the top level of the node, parameter topLevelSlots can be used. This is useful for additional parameters such as ‘icon’, ‘li_attr’ or ‘a_attr’, which jsTree expect to be on the top level of the node.

An example of how to make use of this functionality can be found in the example folder of this library.

**Disclaimer**

These are helper functions from the github version of the shinyTree package which are very helpful to transform dataframes to nested lists and back again. These functions are not yet available (still listed as an enhancement Add data.frame -> shinyTree helper function #8 ) in the lastest version on CRAN v0.2.7 and the github version builds are currently failing. Rather than pulling the dev version of the pacakge to include these functions, adding them here until a stable version is available on CRAN. I claim no credit or ownership of any of this code. All credit should be attributed to the original authors listed on the github repo.

Value

a JSON string representing the data.tree

Note

updateTree and renderTree need an unevaluated JSON string. Hence, this function returns a string rather than the JSON object itself.

Author(s)

Thorn Thaler, thorn.thaler@thothal.at


cusom/CUSOM.ShinyHelpers documentation built on Oct. 23, 2023, 6:50 a.m.