【问题标题】:Why does Shiny not render these Infoboxes inside a tabItem为什么 Shiny 不在 tabItem 中呈现这些信息框
【发布时间】:2018-06-06 15:33:18
【问题描述】:

我目前正在构建我的第一个 Shiny-App,但我遇到了关于 TabItems 的问题。这是我要做的应用程序的屏幕截图。如果我单击 menuSubItem “area1”,然后 plot1 会显示在主面板中,以及一些信息框。我也想在 area2 和 area3 的 tabItems 中有这个布局,但它不起作用。 Shiny 不会渲染它。

这有效:

library("shiny")
library("shinydashboard")
library("tidyverse")
library("dashboardthemes")
library("ggthemes")
library("DT")
library("lubridate")
#-----------------------------------------------------

ui <- dashboardPage(
  #skin = "black",
  dashboardHeader(title = "Basic dashboard", titleWidth = 450,
                  dropdownMenu(type = "notifications",
                               notificationItem(text="test 1", icon("check")),
                               notificationItem(text="test 2", icon("refresh"),status = "warning"))),
  dashboardSidebar(width = 150, collapsed = FALSE, #disable = TRUE um die Sidebar auszuschalten
                   sidebarMenu(id = "tabs",
                               menuItem("Areas", icon = icon("bar-chart-o"), startExpanded = TRUE),
                               menuSubItem("area1", tabName = "tab1", icon = shiny::icon("clipboard-check",lib = "font-awesome"), selected =TRUE),
                               menuSubItem("area2", tabName = "tab2", icon = shiny::icon("pallet", lib = "font-awesome")),
                               menuSubItem("area3", tabName = "tab3", icon = shiny::icon("dolly-flatbed", lib = "font-awesome")),
                               dateInput(inputId = 'dateselection',
                                         label = 'Show this date',
                                         value = Sys.Date(),
                                         language = "de",
                                         max = Sys.Date(),
                                         startview = "year",
                                         weekstart = 1, width = 450),
                               menuItem("Table", tabName = "table1", icon = icon("table"))
                   )
  ),
  dashboardBody(
    ### changing theme
    shinyDashboardThemes(theme = "grey_dark"),
    mainPanel(
      tabItems(
        tabItem(tabName = "tab1",  class='active', 
                h2("area 1"),
                fluidRow(
                  column(width = 8,
                         tabItem(tabName = "tab1", plotOutput("plot1"), width = 8)
                  ), 
                  column(width = 4,
                         tabItem(tabName = "tab1", width = 4,
                                 infoBoxOutput("ordersbox", width = NULL),
                                 infoBoxOutput("progressBox", width = NULL),
                                 infoBoxOutput("approvalBox", width = NULL),
                                 infoBoxOutput("BonusBox", width = NULL))
                  )
                )
        ),
        tabItem(tabName = "tab3",  #class='active', 
                h2("area 2"),width = 12,
                fluidRow(
                  column(width = 8,
                         tabItem(tabName = "tab3", plotOutput("plot3"), width = 8)
                  )#,  # If i uncomment this, the app doesn´t render anymore!!! Why is that so?
                  # column(width = 4,
                  #        tabItem(tabName = "tab3", width = NULL,
                  #                infoBoxOutput("ordersbox", width = NULL),
                  #                infoBoxOutput("progressBox", width = NULL),
                  #                infoBoxOutput("approvalBox", width = NULL),
                  #                infoBoxOutput("BonusBox", width = NULL))
                  #                )
                )
        ),
        tabItem(tabName = "table1", 
                h2("Example Table"),
                width = 8,
                fluidRow(
                  column(width = 8,
                         DT::DTOutput('mytable1')))), #dataTableOutput
        tabItem(tabName = "tab2", 
                h2("area 3"),
                width = 8,
                fluidRow(
                  column(width = 8,
                         plotOutput("plot2"))))
      )   #tabItems
    )    #main Panel
  )      #dashboard body
)        #UI

server <- function(input, output, session){

  # 1. Box
  output$ordersbox <- renderInfoBox({
    infoBox(
      "KPI 1", "120", icon = icon("users", lib = "font-awesome"),
      color = "light-blue", fill =TRUE, width = 3
    )
  }) 

  # 2. Box
  output$progressBox <- renderInfoBox({
    invalidateLater(as.integer(1000))
    infoBox(
      "Time",
      paste(format(Sys.time(), "%H:%M:%S"), "h"), 
      icon = icon("time", lib = "glyphicon"),
      color = "teal", fill =TRUE, width = 3
    )
  })

  # 3. Box
  output$approvalBox <- renderInfoBox({
    infoBox(
      "KPI 2", "120", icon = icon("check-square", lib = "font-awesome"),
      color = "yellow", fill =TRUE,width = 3
    )
  })

  # 4. Box
  output$BonusBox <- renderInfoBox({
    infoBox(
      "KPI 3", "110", icon = icon("info-circle", lib = "font-awesome"),
      color = "red", fill =TRUE, width = 3
    )
  })

  # time
  output$currentTime <- renderText({
    invalidateLater(as.integer(1000))
    paste("The current time is", Sys.time())
  })

  # Table 
  output$mytable1  <- DT::renderDT({ 
    DT::datatable(mpg)
  })

  # Plot1
  output$plot1 <- renderPlot({
    ggplot(mpg, aes(displ, hwy, colour = class)) +  geom_point() + labs(title ="Plot 1")
  })

  # Plot2
  output$plot2 <- renderPlot({
    ggplot(mpg, aes(displ, hwy, colour = class)) +  geom_point() + labs(title ="Plot 2")
  })

  # Plot3
  output$plot3 <- renderPlot({
    ggplot(mpg, aes(displ, hwy)) +  geom_col() + labs(title ="Plot 3")
  })
}

shinyApp(ui, server)

但如果我添加注释代码,那么闪亮不再呈现:

非常感谢任何帮助!我已经尝试了几个小时的更改,但没有任何想法。

【问题讨论】:

    标签: r shiny shinydashboard


    【解决方案1】:

    你不能在两个输出中使用一个元素(“ordersbox”)

    library("shiny")
    library("shinydashboard")
    library("tidyverse")
    #library("dashboardthemes")
    library("ggthemes")
    library("DT")
    library("lubridate")
    #-----------------------------------------------------
    
    ui <- dashboardPage(
      #skin = "black",
      dashboardHeader(title = "Basic dashboard", titleWidth = 450,
                      dropdownMenu(type = "notifications",
                                   notificationItem(text="test 1", icon("check")),
                                   notificationItem(text="test 2", icon("refresh"),status = "warning"))),
      dashboardSidebar(width = 150, collapsed = FALSE, #disable = TRUE um die Sidebar auszuschalten
                       sidebarMenu(id = "tabs",
                                   menuItem("Areas", icon = icon("bar-chart-o"), startExpanded = TRUE),
                                   menuSubItem("area1", tabName = "tab1", icon = shiny::icon("clipboard-check",lib = "font-awesome"), selected =TRUE),
                                   menuSubItem("area2", tabName = "tab2", icon = shiny::icon("pallet", lib = "font-awesome")),
                                   menuSubItem("area3", tabName = "tab3", icon = shiny::icon("dolly-flatbed", lib = "font-awesome")),
                                   dateInput(inputId = 'dateselection',
                                             label = 'Show this date',
                                             value = Sys.Date(),
                                             language = "de",
                                             max = Sys.Date(),
                                             startview = "year",
                                             weekstart = 1, width = 450),
                                   menuItem("Table", tabName = "table1", icon = icon("table"))
                       )
      ),
      dashboardBody(
        ### changing theme
        #shinyDashboardThemes(theme = "grey_dark"),
        mainPanel(
          tabItems(
            tabItem(tabName = "tab1",  class='active', 
                    h2("area 1"),
                    fluidRow(
                      column(width = 8,
                             tabItem(tabName = "tab1", plotOutput("plot1"), width = 8)
                      ), 
                      column(width = 4,
                             tabItem(tabName = "tab1", width = 4,
                                     infoBoxOutput("ordersbox", width = NULL),
                                     infoBoxOutput("progressBox", width = NULL),
                                     infoBoxOutput("approvalBox", width = NULL),
                                     infoBoxOutput("BonusBox", width = NULL))
                      )
                    )
            ),
            tabItem(tabName = "tab3",  #class='active', 
                    h2("area 2"),width = 12,
                    fluidRow(
                      column(width = 8,
                             tabItem(tabName = "tab3", plotOutput("plot3"), width = 8)
                      ),  # If i uncomment this, the app doesn´t render anymore!!! Why is that so?
                       column(width = 4,
                              tabItem(tabName = "tab3", width = NULL,
                                      infoBoxOutput("ordersbox1", width = NULL),
                                      infoBoxOutput("progressBox1", width = NULL),
                                      infoBoxOutput("approvalBox1", width = NULL),
                                      infoBoxOutput("BonusBox1", width = NULL))
                                      )
                    )
            ),
            tabItem(tabName = "table1", 
                    h2("Example Table"),
                    width = 8,
                    fluidRow(
                      column(width = 8,
                             DT::DTOutput('mytable1')))), #dataTableOutput
            tabItem(tabName = "tab2", 
                    h2("area 3"),
                    width = 8,
                    fluidRow(
                      column(width = 8,
                             plotOutput("plot2"))))
          )   #tabItems
        )    #main Panel
      )      #dashboard body
    )        #UI
    
    server <- function(input, output, session){
    
      # 1. Box
      output$ordersbox <- renderInfoBox({
        infoBox(
          "KPI 1", "120", icon = icon("users", lib = "font-awesome"),
          color = "light-blue", fill =TRUE, width = 3
        )
      }) 
    
      # 2. Box
      output$progressBox <- renderInfoBox({
        invalidateLater(as.integer(1000))
        infoBox(
          "Time",
          paste(format(Sys.time(), "%H:%M:%S"), "h"), 
          icon = icon("time", lib = "glyphicon"),
          color = "teal", fill =TRUE, width = 3
        )
      })
    
      # 3. Box
      output$approvalBox <- renderInfoBox({
        infoBox(
          "KPI 2", "120", icon = icon("check-square", lib = "font-awesome"),
          color = "yellow", fill =TRUE,width = 3
        )
      })
    
      # 4. Box
      output$BonusBox <- renderInfoBox({
        infoBox(
          "KPI 3", "110", icon = icon("info-circle", lib = "font-awesome"),
          color = "red", fill =TRUE, width = 3
        )
      })
    
      output$ordersbox1 <- renderInfoBox({
        infoBox(
          "KPI 1", "120", icon = icon("users", lib = "font-awesome"),
          color = "light-blue", fill =TRUE, width = 3
        )
      }) 
    
      # 2. Box
      output$progressBox1 <- renderInfoBox({
        invalidateLater(as.integer(1000))
        infoBox(
          "Time",
          paste(format(Sys.time(), "%H:%M:%S"), "h"), 
          icon = icon("time", lib = "glyphicon"),
          color = "teal", fill =TRUE, width = 3
        )
      })
    
      # 3. Box
      output$approvalBox1 <- renderInfoBox({
        infoBox(
          "KPI 2", "120", icon = icon("check-square", lib = "font-awesome"),
          color = "yellow", fill =TRUE,width = 3
        )
      })
    
      # 4. Box
      output$BonusBox1 <- renderInfoBox({
        infoBox(
          "KPI 3", "110", icon = icon("info-circle", lib = "font-awesome"),
          color = "red", fill =TRUE, width = 3
        )
      })
    
      # time
      output$currentTime <- renderText({
        invalidateLater(as.integer(1000))
        paste("The current time is", Sys.time())
      })
    
      # Table 
      output$mytable1  <- DT::renderDT({ 
        DT::datatable(mpg)
      })
    
      # Plot1
      output$plot1 <- renderPlot({
        ggplot(mpg, aes(displ, hwy, colour = class)) +  geom_point() + labs(title ="Plot 1")
      })
    
      # Plot2
      output$plot2 <- renderPlot({
        ggplot(mpg, aes(displ, hwy, colour = class)) +  geom_point() + labs(title ="Plot 2")
      })
    
      # Plot3
      output$plot3 <- renderPlot({
        ggplot(mpg, aes(displ, hwy)) +  geom_col() + labs(title ="Plot 3")
      })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 谢谢。我认为如果它们不改变,你可以为多个标签集“重用”元素。好吧,我想给我更多代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-26
    • 2020-10-11
    • 2012-12-22
    • 2016-03-10
    • 2013-12-09
    相关资源
    最近更新 更多