闪亮的仪表板和DT表没有显示

时间:2017-12-04 19:08:29

标签: r shiny shinydashboard dt

我有一个仪表板,我想展示一张桌子,但我无法弄清楚为什么我的桌子没有显示。如果我用一些文本替换表格,h2(....)它会显示。我想点击" Species"单击时右侧显示表格。

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)

2 个答案:

答案 0 :(得分:3)

很少有东西可以让你的代码在这里运行。其他贡献者已经注意到夫妇。

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

另一个是使用switchInput中的menuItem可能会混淆app,因为这些不是传递给函数的标准参数。从我的代码中可以看出,这是一个常见的挑战,就是您希望只有在选中“Species”选项卡时才能显示此switchInput。我们可以使用conditionalPanel对此进行说明。为此,我们可以在id = "tabs"中设置sidebarMenu,然后在sidebarMenu中引用此conditionalPanel

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)
  })

})

答案 1 :(得分:1)

您需要更改/添加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)