【问题标题】:Shiny dashboard and DT table not showing闪亮的仪表板和 DT 表未显示
【发布时间】:2018-05-18 07:52:54
【问题描述】:

我有一个仪表板,我想在其中显示一个表格,但我不知道为什么我的表格没有显示。例如,如果我用一些文本替换表格,h2(....) 它会显示。我想单击“物种”并在单击时将表格显示在右侧。

library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(DT)

ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(sidebarMenu(
    menuItem(
      "Species",
      tabName = "Species",
      icon = NULL,
      switchInput(
        inputId = "long1",
        onLabel = "Go",
        offLabel = "NoGo",
        value = T
      ),
      actionButton("Gobtn", "Get data"),
      menuItem("Specs", tabName = "Specs", icon = NULL)
    )
  )),

  dashboardBody(tabItems(
    tabItem(tabName = "Species",
            DT::renderDataTable("Table1")),
    tabItem(tabName = "Specs",
            h2("Hi"))
  ))
)

server.r

  server <- shinyServer(function(input, output, session) {
  output$Table1 <- DT::renderDataTable({
    iris
  })
})

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny shinydashboard dt


    【解决方案1】:

    让您的代码在此处启动并运行的事情很少。其他贡献者已经注意到了情侣。

    我们需要在UI端使用DT::dataTableOutput("Table1"),因为renderDataTable在这里不起作用,那是服务器端的功能。

    另一个是在menuItem 中使用switchInput 可能会混淆应用程序,因为这些不是传递给函数的标准参数。从您的代码中我可以看到,这是一个常见的挑战,您希望仅在选择“物种”选项卡时才能显示此switchInput。我们可以使用conditionalPanel 来解决这个问题。为此,我们可以在sidebarMenu 中设置id = "tabs",然后在conditionalPanel 中引用此sidebarMenu

    conditionalPanel(
      condition = "input.tabs== 'Species' ",
      switchInput(
        inputId = "long1",
        onLabel = "Go",
        offLabel = "NoGo",
        value = T
      )
    )
    

    最后,我更改了 ui.R 和 server.R 的布局,因为应用程序不需要 shinyApp 函数来处理服务器和 ui 文件。这就是我布置仪表板的方式。它可能会向您展示一些其他可能的方式,您可以在 Shiny 中使用应用程序结构,但同样您也可以将更改与基本布局对齐。

    ui.R

    header <- dashboardHeader(title = "Basic dashboard")
    
    sidebar <- dashboardSidebar(
      sidebarMenu(id = "tabs",
        menuItem(
          "Species",
          tabName = "Species",
          icon = NULL),
    
        conditionalPanel(
          condition = "input.tabs== 'Species' ",
          switchInput(
            inputId = "long1",
            onLabel = "Go",
            offLabel = "NoGo",
            value = T
          )
        ),
    
    
        actionButton("Gobtn", "Get data"),
    
        menuItem("Specs", tabName = "Spec", icon = NULL)
      )
    )
    
    body <- dashboardBody(
      tabItems(
        tabItem(tabName = "Species",
                DT::dataTableOutput("Table1")),
    
        tabItem(tabName = "Spec",
                h2("Hi"))
      )
    )
    
    dashboardPage(skin = "blue", header = header, sidebar = sidebar, body = body)
    

    server.R

    library(shiny)
    library(shinydashboard)
    library(shinyWidgets)
    library(DT)
    
    shinyServer(function(input, output, session){
    
      output$Table1 <- DT::renderDataTable({
        datatable(iris)
      })
    
    })
    

    【讨论】:

    • 谢谢。我猜不可能让菜单项可折叠?
    • 我不完全确定你的意思。但是,是的,您可以使用 menuSubItems 或将 javascript 传递到脚本/使用其他菜单类型来制作可折叠菜单。
    【解决方案2】:

    您需要更改/添加dashboardBody 的某些部分,请参阅Using shiny modules and shinydashboard: shiny.tag error

    library(shiny)
    library(shinydashboard)
    library(shinyWidgets)
    library(DT)
    
    ui <- dashboardPage(
      dashboardHeader(title = "Basic dashboard"),
      dashboardSidebar(sidebarMenu(
        menuItem(
          "Species",
          tabName = "Species",
          icon = NULL,
    
          switchInput(
            inputId = "long1",
            onLabel = "Go",
            offLabel = "NoGo",
            value = T
          ),
          actionButton("Gobtn", "Get data")
        )
      )),
    
      dashboardBody(tags$div(
        tabName = "Species",
        fluidRow(box(DT::dataTableOutput("Table1"))), class = "tab-content"
      ))
      )
    

    server.r

    server <- shinyServer(function(input, output, session) {
      output$Table1 <- DT::renderDataTable({
        iris
      })
    })
    
    shinyApp(ui, server)
    

    【讨论】:

    • 抱歉,我在 icon = NULL 后遗漏了一个 ","。我稍微更新了代码,问题是当我单击它时,我希望在“物种”选项卡下显示该表。现在,当我单击选项卡时,会出现按钮选项,但右侧没有任何变化。
    • @user6617474 你的项目完美无缺,但是如果我有几个标签,你能告诉我怎么做吗?因为当我增加标签数量时我无法让它工作
    猜你喜欢
    • 2019-03-28
    • 2019-05-11
    • 2015-04-22
    • 1970-01-01
    • 2019-02-12
    • 2016-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多