Search code examples
rshinydiagrammer

diagrammeR (grViz) is looking for the data from environment rather than from the values inside shiny


I have below setup for diagram, the graph can only produces the below graph if I have data value in the environment, it's not looking at the data value I specified in the server at all. What should I do differently?

enter image description here

library(DiagrammeR)
library(shiny)

ui <- fluidPage(
  grVizOutput("parameter_plot"),
  numericInput("some_values", label = "Some values",
               value = 1, min = 1, max = 100, step = 1)
)

server <- function(input, output, session) {
  # Define some sample data
  output$parameter_plot <- renderGrViz({
    # Define some sample data
    data = tibble(a=1000,b=input$some_values,c=1000,d=1000,e=1000,f=1,g=1,h=1)
    
    
    
    DiagrammeR::grViz("
digraph graph2 {

graph [layout = dot]

# node definitions with substituted label text
node [shape = rectangle, style = filled]

node [fillcolor = lightblue]
a [label = '@@1']

node [fillcolor = Beige]
b [label = '@@2']
c [label = '@@3']
d [label = '@@4']

node [fillcolor = mediumpurple1]
e [label = '@@5']
f [label = '@@6']
g [label = '@@7']

node [fillcolor = darkseagreen1]
h [label = '@@8']
i [label = '@@9']


  subgraph cluster1 {
    node [fixedsize = true, width = 3]
    b -> {c d}
  }
  
  subgraph cluster2 {
    node [fixedsize = true, width = 3]
    e -> f -> g -> e
  }
  
  subgraph cluster3 {
    node [fixedsize = true, width = 3]
    h
    i
  }

a -> b 
a -> e
f -> i
e -> h

}

")
  })
}

shinyApp(ui, server)


Solution

  • If you specify the footnotes at the end correctly it should work. In the example below I use glue::glue to insert code into the string. Part of label 1 comes from a data.frame and a part of label 6 comes from a numericInput and can be adjusted interactively.

    library(DiagrammeR)
    library(shiny)
    library(dplyr)
    library(glue)
    
    ui <- fluidPage(
      grVizOutput("parameter_plot"),
      numericInput("some_values", label = "Some values",
                   value = 1, min = 1, max = 100, step = 1)
    )
    
    server <- function(input, output, session) {
      # Define some sample data
      output$parameter_plot <- renderGrViz({
        
        print(input$some_values)
        # Define some sample data
        data = tibble(a = 696,
                      b = input$some_values,
                      c = 1000,
                      d = 1000,
                      e = 1000,
                      f = 1,
                      g = 1,
                      h = 1)
        
        
        
        DiagrammeR::grViz(glue::glue(
          .open = "{{",
          .close = "}}", 
          
          "digraph graph2 {
    
    graph [layout = dot]
    
    # node definitions with substituted label text
    node [shape = rectangle, style = filled]
    
    node [fillcolor = lightblue]
    a [label = '@@1']
    
    node [fillcolor = Beige]
    b [label = '@@2']
    c [label = '@@3']
    d [label = '@@4']
    
    node [fillcolor = mediumpurple1]
    e [label = '@@5']
    f [label = '@@6']
    g [label = '@@7']
    
    node [fillcolor = darkseagreen1]
    h [label = '@@8']
    i [label = '@@9']
    
      subgraph cluster1 {
        node [fixedsize = true, width = 3]
        b -> {c d}
      }
      
      subgraph cluster2 {
        node [fixedsize = true, width = 3]
        e -> f -> g -> e
      }
      
      subgraph cluster3 {
        node [fixedsize = true, width = 3]
        h
        i
      }
    
    a -> b 
    a -> e
    f -> i
    e -> h
    
    }
    
    [1]: '1. Est.Demand (n = {{data$a}})'
    [2]: 'Patient leaves pathway'
    [3]: 'NAS: Discharged (n = 78)'
    [4]: '2. ROTT (n = 8)'
    [5]: '4. Clinical Slots Used (n = 665)'
    [6]: 'Some values (n = {{input$some_values}})'
    [7]: '7. NASL: Rebooked (n = 55)'
    [8]: '6. NASL (n = 14)'
    [9]: '8. Expected Attendences (n = 596)'
    
    "))
        
      })
    }
    
    shinyApp(ui, server)