Formatter functions set on a Node act as a default formatter when printing and using the Get method. The formatter is inherited, meaning that whenever Get fetches an attribute from a Node, it checks on the Node or on any of its ancestors whether a formatter is set.

SetFormat(node, name, formatFun)

Arguments

node

The node on which to set the formatter

name

The attribute name for which to set the formatter

formatFun

The formatter, i.e. a function taking a value as an input, and formatting returning the formatted value

See also

Get

print.Node

Examples

data(acme)
acme$Set(id = 1:(acme$totalCount))
SetFormat(acme, "id", function(x) FormatPercent(x, digits = 0))
SetFormat(Climb(acme, "IT"), "id", FormatFixedDecimal)
print(acme, "id")
#>                           levelName     id
#> 1  Acme Inc.                         100 %
#> 2   ¦--Accounting                    200 %
#> 3   ¦   ¦--New Software              300 %
#> 4   ¦   °--New Accounting Standards  400 %
#> 5   ¦--Research                      500 %
#> 6   ¦   ¦--New Product Line          600 %
#> 7   ¦   °--New Labs                  700 %
#> 8   °--IT                            8.000
#> 9       ¦--Outsource                 9.000
#> 10      ¦--Go agile                 10.000
#> 11      °--Switch to R              11.000
# Calling Get with an explicit formatter will overwrite the default set on the Node:
print(acme, id = acme$Get("id", format = function(x) paste0("id:", x)))
#>                           levelName    id
#> 1  Acme Inc.                         id:1
#> 2   ¦--Accounting                    id:2
#> 3   ¦   ¦--New Software              id:3
#> 4   ¦   °--New Accounting Standards  id:4
#> 5   ¦--Research                      id:5
#> 6   ¦   ¦--New Product Line          id:6
#> 7   ¦   °--New Labs                  id:7
#> 8   °--IT                            id:8
#> 9       ¦--Outsource                 id:9
#> 10      ¦--Go agile                 id:10
#> 11      °--Switch to R              id:11

# Or, to avoid formatters, even though you set them on a Node:
print(acme, id = acme$Get("id", format = identity))
#>                           levelName id
#> 1  Acme Inc.                         1
#> 2   ¦--Accounting                    2
#> 3   ¦   ¦--New Software              3
#> 4   ¦   °--New Accounting Standards  4
#> 5   ¦--Research                      5
#> 6   ¦   ¦--New Product Line          6
#> 7   ¦   °--New Labs                  7
#> 8   °--IT                            8
#> 9       ¦--Outsource                 9
#> 10      ¦--Go agile                 10
#> 11      °--Switch to R              11