简体   繁体   中英

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. 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.

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. 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. We can account for this using conditionalPanel . To do this, we can set id = "tabs" within the sidebarMenu and then reference this sidebarMenu within the conditionalPanel :

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. 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.

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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