This functions allows to convert nested hierarchies into other data structures.
hier_convert(tree, as = "df")
a (nested) hierarchy created using hier_create()
or modified using hier_add()
, hier_delete()
or hier_rename()
.
(character) specifying the export format. Possible choices are:
"df"
: a data.frame
with two columns. The first
columns contains a string containing as many @
as the level of the
node in the string (e.g @
corresponds to the overall
total while @
would be all codes contributing to the total.
The second column contains the names of the levels.
"dt"
: like the df
-version but this result is
converted to a data.table
"argus"
: used to create hrc-files suitable for tau-argus
"json"
: json format suitable e.g. as input for
the shinyTree package.
"code"
: code required to generate the hierarchy
"sdc"
: a list
which is a suitable input for sdcTable
h <- hier_create(root = "Total", nodes = LETTERS[1:2])
h <- hier_add(h, root = "A", nodes = c("a1", "a2"))
h <- hier_add(h, root = "B", nodes = c("b1", "b2"))
h <- hier_add(h, root = "b1", nodes = "b1a")
hier_display(h)
#> Total
#> ├─A
#> │ ├─a1
#> │ └─a2
#> └─B
#> ├─b1
#> │ └─b1a
#> └─b2
# required code to build the hierarchy
hier_convert(h, as = "code")
#> [1] "library(sdcHierarchies)"
#> [2] "tree <- hier_create(root = 'Total', nodes = c('A', 'B'))"
#> [3] "tree <- hier_add(tree = tree, root = 'A', nodes = c('a1', 'a2'))"
#> [4] "tree <- hier_add(tree = tree, root = 'B', nodes = c('b1', 'b2'))"
#> [5] "tree <- hier_add(tree = tree, root = 'b1', nodes = 'b1a')"
#> [6] "print(tree)"
#> attr(,"hier_convert")
#> [1] TRUE
#> attr(,"hier_format")
#> [1] "code"
# data.frame
hier_convert(h, as = "df")
#> level name
#> 1 @ Total
#> 2 @@ A
#> 3 @@@ a1
#> 4 @@@ a2
#> 5 @@ B
#> 6 @@@ b1
#> 7 @@@@ b1a
#> 8 @@@ b2