Using R data.tree from within a Shiny app

260 views Asked by At

I am trying to add some code to an existing shiny app that would display the graphic version of a data.tree structure. The code below successfully creates the data.tree the way I would like it displayed within a window in my shiny app. However, I cannot find any way to render 'theTree' to make it appear as it does in the RStudio viewer.

library(data.tree)

treedata <- data.frame(c("Product A", "Product A", "Product A", "Product A", "Product A", "Product A"),
                       c("Sub-A", "Sub-A", "Sub-A", "Sub-B", "Sub-B", "Sub-B"),
                       c("Comp-1", "Comp-1", "*", "Comp-2", "Comp-2", "*"),
                       c("SubCompA", "SubCompB", "SubCompC", "SubCompA", "SubCompB", "SubCompC"))
names(treedata) <- c("ProductName", "SubProjects", "Component" ,"SQComponents")

product <- "Product A"

treedata$pathString <- paste(product,
                             treedata$SubProjects,
                             treedata$Component,
                             treedata$SQComponents,
                             sep = "/")

theTree <- as.Node(treedata)

SetGraphStyle(theTree, rankdir = "TB")
SetEdgeStyle(theTree, arrowhead = "vee", color = "grey35", penwidth = 2)
SetNodeStyle(theTree, style = "filled,rounded", shape = "box", fillcolor = "GreenYellow",
             fontname = "helvetica",
             fontcolor = "black",
             tooltip = GetDefaultTooltip)

p <- plot(theTree)
print(p)

1

There are 1 answers

0
Stéphane Laurent On BEST ANSWER

This is a 'grViz' HTML widget. Here is how to render it in Shiny:

library(shiny)
library(DiagrammeR)
library(data.tree)

treedata <- data.frame(
  c("Product A", "Product A", "Product A", "Product A", "Product A", "Product A"),
  c("Sub-A", "Sub-A", "Sub-A", "Sub-B", "Sub-B", "Sub-B"),
  c("Comp-1", "Comp-1", "*", "Comp-2", "Comp-2", "*"),
  c("SubCompA", "SubCompB", "SubCompC", "SubCompA", "SubCompB", "SubCompC")
)
names(treedata) <- 
  c("ProductName", "SubProjects", "Component" ,"SQComponents")
product <- "Product A"
treedata$pathString <- paste(product,
                             treedata$SubProjects,
                             treedata$Component,
                             treedata$SQComponents,
                             sep = "/")
theTree <- as.Node(treedata)
SetGraphStyle(theTree, rankdir = "TB")
SetEdgeStyle(theTree, arrowhead = "vee", color = "grey35", penwidth = 2)
SetNodeStyle(theTree, style = "filled,rounded", shape = "box", 
             fillcolor = "GreenYellow",
             fontname = "helvetica",
             fontcolor = "black",
             tooltip = GetDefaultTooltip)

p <- plot(theTree)

ui <- fluidPage(
  grVizOutput("tree")
)

server <- function(input, output, session){
  output[["tree"]] <- renderGrViz(p)
}

shinyApp(ui, server)