library(data.tree) acme <- Node$new("Acme Inc.") accounting <- acme$AddChild("Accounting") software <- accounting$AddChild("New Software") standards <- accounting$AddChild("New Accounting Standards") research <- acme$AddChild("Research") newProductLine <- research$AddChild("New Product Line") newLabs <- research$AddChild("New Labs") it <- acme$AddChild("IT") outsource <- it$AddChild("Outsource") agile <- it$AddChild("Go agile") goToR <- it$AddChild("Switch to R") acme$Accounting$`New Software`$cost <- 1000000 acme$Accounting$`New Accounting Standards`$cost <- 500000 acme$Research$`New Product Line`$cost <- 2000000 acme$Research$`New Labs`$cost <- 750000 acme$IT$Outsource$cost <- 400000 acme$IT$`Go agile`$cost <- 250000 acme$IT$`Switch to R`$cost <- 50000 Cost <- function(node) { result <- node$cost if(length(result) == 0) result <- sum(sapply(node$children, Cost)) return (result) } acme$Do(function(node) node$cost <- Cost(node), filterFun = isNotLeaf) acme$Do(function(node) node$cumCost <- Cumulate(node, attribute = "cost",aggFun = sum)) print(acme, "cost", "cumCost") #print(acme, "p", "cost")