当我在 Shiny 应用程序中添加新的 fluidRow 时,R 闪亮的仪表图消失了?

Posted

技术标签:

【中文标题】当我在 Shiny 应用程序中添加新的 fluidRow 时,R 闪亮的仪表图消失了?【英文标题】:R shiny gauge graph disappear when I add new fluidRow in Shiny app? 【发布时间】:2021-10-19 04:36:09 【问题描述】:

我制作了一个闪亮的应用程序,但当我添加一个新的fluidRow 时,我遇到了图表消失的问题。这是一个小例子

library(shinydashboard)
header <- dashboardHeader(title = 'Name')
sidebar <- dashboardSidebar()
body <- dashboardBody(
fluidRow(
    box(widht = 12, title = 'Group', textOutput('group_name'))
),
fluidRow(
    box(width = 4, title = 'Frim', textOutput('firm_name')),
    box(width = 4, title = 'INN', textOutput('firm_inn')),
    box(width = 4, title = 'Branch', textOutput('firm_branch'))
),
fluidRow(
    box(status = 'primary', height = '250px', width = 4, title = 'name1', plotlyOutput('firm_limits_utiliz'), solidHeader = TRUE),
    box(status = 'primary', height = '250px', width = 4, title = 'name2', plotlyOutput('firm_limits_remains'), solidHeader = TRUE),
    box(status = 'warning', height = '250px', width = 4, title = 'name3',  tableOutput('group_limits') , solidHeader = TRUE,
        style = "overflow-x: scroll;")
) )
ui <- dashboardPage(body = body, header = header, sidebar = sidebar, skin = 'blue')
server <- function(input, output) 

output$group_name <- renderText('Shell')

output$firm_name <- renderText('Shell ltd')

output$firm_inn <- renderText('770565479')

output$firm_branch <- renderText('Oil and Gas')

output$firm_limits_utiliz <- renderPlotly(
    fig <- plot_ly(
        domain = list(x = c(0, 1), y = c(0, 1)),
        value = 270,
        # title = list(text = "Speed"),
        type = "indicator",
        mode = "gauge+number",
        height = 197, width = 393) 
    fig <- fig %>%
        layout(margin = list(l=20,r=30))
    
    fig
)


output$group_limit_utiliz <- renderPlotly(
    fig <- plot_ly(
        domain = list(x = c(0, 1), y = c(0, 1)),
        value = 270,
        # title = list(text = "Speed"),
        type = "indicator",
        mode = "gauge+number",
        height = 197, width = 393) 
    fig <- fig %>%
        layout(margin = list(l=20,r=30))
    
    fig
)


# Run the application 
shinyApp(ui = ui, server = server)

如果我运行这段代码,一切都会顺利。无花果图显示在仪表板上。 但是!

当我添加一个新的fluidRow 时,无花果图消失了。例如让我为您提供body 部分代码:

 body <- dashboardBody(
fluidRow(
    box(widht = 12, title = 'Group', textOutput('group_name'))
),
fluidRow(
    box(width = 4, title = 'Firm', textOutput('firm_name')),
    box(width = 4, title = 'INN', textOutput('firm_inn')),
    box(width = 4, title = 'Branch', textOutput('firm_branch'))
),
fluidRow(
    box(status = 'primary', height = '250px', width = 4, title = 'name1', plotlyOutput('firm_limits_utiliz'), solidHeader = TRUE),
    box(status = 'primary', height = '250px', width = 4, title = 'name2', plotlyOutput('firm_limits_remains'), solidHeader = TRUE),
    box(status = 'warning', height = '250px', width = 4, title = 'name3',  tableOutput('group_limits') , solidHeader = TRUE,
        style = "overflow-x: scroll;")
),
# Here i a new fluidRow
fluidRow(
        box(status = 'primary', height = '250px', width = 4, title = 'name6', plotlyOutput('group_limit_utiliz'), solidHeader = TRUE),
        box(status = 'primary', height = '250px', width = 4, title = 'name4', plotlyOutput('gtoup_limit_remains'), solidHeader = TRUE),
        box(status = 'warning', height = '250px', width = 4, title = 'name5',  tableOutput('group_limits') , solidHeader = TRUE,
            style = "overflow-x: scroll;")
        
    )
 )

如您所见,有一个新的fluidRow。在这种情况下,带有firm_limits_utiliz id 图的fig 消失了。 怎么了?

【问题讨论】:

【参考方案1】:

如果您使用另一个变量名创建输出副本,它会起作用:

#new fluidRow

  fluidRow(
    box(status = 'primary', height = '250px', width = 4, title = 'name6', plotlyOutput('group_limit_utiliz2'), solidHeader = TRUE),
    box(status = 'primary', height = '250px', width = 4, title = 'name4', plotlyOutput('gtoup_limit_remains2'), solidHeader = TRUE),
    box(status = 'warning', height = '250px', width = 4, title = 'name5',  tableOutput('group_limits2') , solidHeader = TRUE,
        style = "overflow-x: scroll;")

  )

输出:

  output$group_limit_utiliz2 <- renderPlotly(
    fig2 <- plot_ly(
      domain = list(x = c(0, 1), y = c(0, 1)),
      value = 270,
      # title = list(text = "Speed"),
      type = "indicator",
      mode = "gauge+number",
      height = 197, width = 393) 
    fig2 <- fig2 %>%
      layout(margin = list(l=20,r=30))
    
    fig2
  )

请注意,我在所有变量名称中都添加了 2,但其他一切都相同。

【讨论】:

以上是关于当我在 Shiny 应用程序中添加新的 fluidRow 时,R 闪亮的仪表图消失了?的主要内容,如果未能解决你的问题,请参考以下文章

R/Shiny 中的可拖动折线图

图像未在 Shiny 应用 R 中显示

当我有选择 > 1000 时,Shiny 不会向我显示整个 selectInput

如何在 Shiny 中更改选择器输入的字体大小?

R Shiny:如何使多个元素在添加/删除按钮上下文中具有反应性?

在 Shiny 中使用 rhandsontable 时出错