Tree plot displays in R, but not in R-Shiny

I am able to use the data.tree package to produce a plot. Here is an example of a plot:

library(data.tree)

org <- Node$new("Parent")
org$AddChild("Child_1")
org$AddChild("Child_2")

plot(org)

However, I am not able to render this plot in R-Shiny. I have been able to render most other plots in Shiny. What can I do to render this plot, and why is it not showing up? Here is my Shiny code:

library(shiny); library(data.tree)

ui <- fluidPage(
    mainPanel(plotOutput("orgplot") )
)

server <- function(input, output){
  rv <- reactiveValues()

  org <- Node$new("Parent")
  org$AddChild("Child_1")
  org$AddChild("Child_2")

  output$orgplot <- renderPlot({ plot(org)})
}

shinyApp(ui = ui, server = server)         

Answers


plot(org) generate widget of class grViz So you can use renderGrViz to show plot in shiny.

Like ( textInput used for example of change name of "parent")

  library(shiny); 
library(data.tree)
library(DiagrammeR)
ui <- fluidPage(
  mainPanel(grVizOutput("xx")   ),
  textInput("parent","parent","parent")
)

server <- function(input, output){
output$xx=renderGrViz({
  org <- Node$new(input$parent)
  org$AddChild("Child_1")
  org$AddChild("Child_2")
  grViz(ToGraphViz(org),engine = "dot")
  })
}


shinyApp(ui = ui, server = server) 

Update add node dynamically

If You want to add nodes dynamically you can try to add child to node by name( you need some checks to avoid names duplicate)

New functions to draw chart get from @rpm answer

 ui <- fluidPage(
  mainPanel(uiOutput("add_child_ui"),
            grVizOutput("xx")   )
)

server <- function(input, output){
  #Create reative value to app
  vv=reactiveValues(org=NULL,names=NULL)

  #create main tree
  observe({
    vv$org <- Node$new("Parent1")
    vv$org$AddChildNode(child = Node$new("1"))
    vv$names=vv$org$Get('name') # get names of main tree
  })

  output$add_child_ui=renderUI({
    list(
    wellPanel(
      selectInput("Name_to_change","Name_to_change",vv$names),
      textInput("new_name","new_name",""),
      actionButton("Change_name","Change_name")
    ),
    wellPanel(
      selectInput("Parent_name","Parent_name",vv$names),
      textInput("new_node_name","new_node_name",""),
      actionButton("add_child","add_child")
    ))
  })
  observeEvent(input$Change_name,{

    aa=FindNode(node=vv$org,name = input$Name_to_change) 
    aa$name=input$new_name # Change name
    vv$names=vv$org$Get('name')# get names of new tree

    #re-generate chart
    output$xx=renderGrViz({

      grViz(DiagrammeR::generate_dot(ToDiagrammeRGraph(vv$org)),engine = "dot")
    })
  })

  observeEvent(input$add_child,{

    FindNode(node=vv$org,name = input$Parent_name)$AddChildNode(Node$new(input$new_node_name)) # add child
    vv$names=vv$org$Get('name')# get names of new tree

    #re-generate chart
    output$xx=renderGrViz({

      grViz(DiagrammeR::generate_dot(ToDiagrammeRGraph(vv$org)),engine = "dot")
    })
  })

  output$xx=renderGrViz({

    grViz(DiagrammeR::generate_dot(ToDiagrammeRGraph(vv$org)),engine = "dot")
  })
}


shinyApp(ui = ui, server = server) 

This code does not work as the ToGraphViz function has been retired from DiagrammeR and replaced with ToDiagrammerGraph. The following change works.

#  grViz(ToGraphViz(org),engine = "dot")
grViz(DiagrammeR::generate_dot(ToDiagrammeRGraph(org)))

Also, "parent" is a reserved word so I replaced it "parent2".


Need Your Help

Node.js formidable rename uploaded file

node.js fs formidable

Trying to finish the book "The Node beginner book" and the last thing I need is to implement modifying a request handler, which shows an image, renamed after a user has uploaded it, I'm using node-