简体   繁体   English

闪亮的仪表盘和DT表未显示

[英]Shiny dashboard and DT table not showing

I have a dashboard where I would like to show a table, but I cant figure out why my table is not showing. 我有一个仪表板,我想在其中显示表格,但是我无法弄清楚为什么我的表格没有显示。 If I replace the table for example with some text, h2(....) it does show. 例如,如果我用一些文本替换表格,则会显示h2(....) I would like to click on "Species" and have the table show on the right when clicking it. 我想单击“种类”,并在单击时在右侧显示表格。

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)

Few things to get your code up and running here. 很少有什么可以让您的代码在这里启动和运行。 Couple have been noted by other contributors. 夫妇已经被其他贡献者注意到。

We need to use DT::dataTableOutput("Table1") on the UI side as renderDataTable will not work here, that is the server side function. 我们需要在UI端使用DT::dataTableOutput("Table1") ,因为renderDataTable在这里不起作用,即服务器端函数。

The other would be that using the switchInput within the menuItem may confused the app, as these are not standard parameters to pass into the function. 另一个可能是,在menuItem中使用switchInput可能会使应用程序感到困惑,因为这些不是传递给函数的标准参数。 From what I can see from your code, which is a common challenge, is that you want to be able to show this switchInput only when the 'Species' tab is selected. 从我的代码中可以看到,这是一个常见的挑战,那就是您希望仅在选择“ Species”选项卡时才能显示此switchInput We can account for this using conditionalPanel . 我们可以使用conditionalPanel解决这个conditionalPanel To do this, we can set id = "tabs" within the sidebarMenu and then reference this sidebarMenu within the conditionalPanel : 为此,我们可以在sidebarMenu设置id = "tabs" ,然后在conditionalPanel引用此sidebarMenu

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

To finish, I have altered the layouts of the ui.R and server.R, as the shinyApp function was not needed for the app to work with the server and ui files. 最后,我已经更改了ui.R和server.R的布局,因为应用程序不需要与服务器和ui文件一起使用shinyApp函数。 This is how I lay out my dashboards. 这就是我布置仪表板的方式。 It may show you a few other possible ways you can use the app structure within Shiny, but equally you could just align the changes to the basic layout. 它可能向您展示了在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)
  })

})

You need to change/add some part of the dashboardBody , see Using shiny modules and shinydashboard: shiny.tag error 您需要更改/添加dashboardBody某些部分,请参阅使用发光模块和shinydashboard:shiny.tag错误

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM