【问题标题】:diagrammeR (grViz) is looking for the data from environment rather than from the values inside shinydiagrammeR (grViz) 正在从环境中寻找数据,而不是从闪亮的值中寻找数据
【发布时间】:2021-08-04 06:35:09
【问题描述】:

我有以下图表设置,如果我在环境中有data 值,该图表只能生成下面的图表,它根本不查看我在服务器中指定的数据值。我应该怎么做?

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)

【问题讨论】:

    标签: r shiny diagrammer


    【解决方案1】:

    如果您正确指定末尾的脚注,它应该可以工作。在下面的示例中,我使用glue::glue 将代码插入到字符串中。标签 1 的一部分来自data.frame,标签 6 的一部分来自numericInput,可以交互调整。

    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)
    

    【讨论】:

    • 感谢您的回答,但如果我也需要标签,而不仅仅是数字,我该怎么做?请参考我的问题中的图片。我试过粘贴,但没有用。
    • 这当然是可能的,我稍后会调整我的答案,但是,我不清楚您要根据input$some_values更改什么标签。
    • 当然谢谢,我希望 input$some_values 类似于:“Some values: (n=input$some_values)”,基本上我希望变量值与固定文本相关联,请让我知道这是否足够清楚,我很抱歉在我的问题中没有说清楚,稍后会编辑它。
    • 有可能,我稍后会编辑我的答案。
    • @SubaruSpirit:我更新了我的答案。它现在应该可以按预期工作。在定义标签时,将它们包装在 '' 中很重要,就像在 b [label = '@@2'] 中一样。起初我没有发现这一点,但没有它,我遇到了很多错误。
    猜你喜欢
    • 2018-12-13
    • 1970-01-01
    • 1970-01-01
    • 2023-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    相关资源
    最近更新 更多