简体   繁体   中英

Shiny dataTableOutput won't show up if its is under tabItem

My Shiny APP has a simple structure looks like:

ui <- fluidPage(
  dashboardPage(
    dashboardHeader(title = "My App"),

    dashboardSidebar(
      sidebarMenu(
        menuItem("Dashboard", tabName = "dashboard",
                 selectInput('location', 'Please Select a Location', choices= c("A", "B", "C")),
                 downloadButton(outputId = 'download', lable = "Downlaod"),
        menuItem("Widgets", tabName = "widgets", badgeLabel = "new", badgeColor = "green")
    )),

    # dashboardBody first try (works no problem):
    dashboardBody(DT::dataTableOutput(outputId = 'mytable'))

    # dashboardBody second try (data table does not appear):
    dashboardBody(
      tabItems(
        tabItem(tabName = "dashboard",
          DT::dataTableOutput(outputId = 'mytable')),

        tabItem(tabName = "widgets",
          h2("Widgets tab content"))
  )))


server <- shinyServer(function(input, output, session){
  output$mytable<- DT::renderDataTable({```some calculation```})

  output$downloadcsv <- downloadHandler(
    filename = function(){paste0(sys.Date(),'-mytable.csv')},
    content = function(file) {write.csv(```the table from renderDataTable step```, file)}
)}

As you can see, the app includes two different "pages" where the first one is a data table depends on the selectInput .

My app runs perfectly if I don't wrap it with tabItem . However, once I write it under tabItem , the app only shows "Widgets tab content" which is the content of the second tab and does not populate the data table at all (while the download button still works).

I've also tried to add class='active' behind tabName = "dashboard" , but it still doesn't work. Plus, I'm not getting any error message.

I'm wondering if anyone knows which step did I go wrong? Any help would be appreciated!

The problem lies in the placement of the table. I have rendered input options outside the menuItem. Check this code below

ui <- fluidPage(
  dashboardPage(
    dashboardHeader(title = "My App"),



dashboardSidebar(
  sidebarMenu(
    selectInput('location', 'Please Select a Location', choices= c("A", "B", "C")),
    downloadButton(outputId = 'download.csv', lable = "Downlaod"),
    menuItem("Dashboard", tabName = "dashboard"),

             menuItem("Widgets", tabName = "widgets", badgeLabel = "new", badgeColor = "green")


    )),

  # dashboardBody first try (works no problem):
  #dashboardBody(DT::dataTableOutput(outputId = 'mytable'))

  #dashboardBody second try (data table does not appear):
  dashboardBody(
    tabItems(
      tabItem(tabName = "dashboard",
              DT::dataTableOutput(outputId = 'mytable')),

      tabItem(tabName = "widgets",
              h2("Widgets tab content"))
    ))
))


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

  output$mytable<- DT::renderDataTable({DT::datatable(head(iris))})

  output$downloadcsv <- downloadHandler(
    filename = function(){paste0(sys.Date(),'-mytable.csv')},
    content = function(file) {write.csv(head(iris), file)}
  )}

  shinyApp(ui=ui, server=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