Search code examples
rshinyshinydashboarddt

DT::renderDT not working when more than one tabPanel


This code fails to produce a table in the tabPanel "table", however, when I comment one of the other tabPanel (e.g. "Overview") it works fine. Any idea why that might be?

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

ui <- 
  navbarPage("App Title",
             
             tabPanel("Overview",
                      sidebarLayout(position = "right", fluid = T,
                                    sidebarPanel(width = 3,
                                                 
                                                 radioButtons("percentage_select", "Percentage of:",
                                                              c("All Publications" = "p",
                                                                "All SDG Publications" = "l")
                                                 ),
                                                 
                                                 checkboxGroupInput("sdg_select", "SDG:",
                                                                    choices = c(1, 2),
                                                                    selected = c(1, 2),
                                                                    inline = T
                                                 ),
                                                 
                                                 sliderInput("year_range", "Years:",
                                                             min = 2011,
                                                             max = 2020,
                                                             value = range(2011, 2020),
                                                             round = T,
                                                             step = 1,
                                                             ticks = T
                                                 ),
                                                 
                                                 actionButton('reset_filters', 'Reset Filters',
                                                              icon = icon("sync")
                                                 )
                                    ),
                                    
                                    mainPanel(
                                      plotOutput("plot")
                                    )
                      )
             ),
             
             tabPanel("Cumulative",
                      sidebarLayout(position = "right", fluid = T,
                                    sidebarPanel(width = 3,
                                                 
                                                 radioButtons("percentage_select", "Indicator:",
                                                              c("Percentage" = "p",
                                                                "Absolute" = "l")
                                                 ),
                                                 
                                                 checkboxGroupInput("baseline_select", "Show average:",
                                                                    choices = c("World", "Univ. "),
                                                                    selected = c("World", "Univ. "),
                                                                    inline = F
                                                 ),
                                                 
                                                 checkboxGroupInput("sdg_select", "SDG:",
                                                                    choices = c(1, 2),
                                                                    selected = c(1, 2),
                                                                    inline = T
                                                 ),
                                                 
                                                 sliderInput("year_range", "Years:",
                                                             min = 2011,
                                                             max = 2020,
                                                             value = range(2011, 2020),
                                                             round = T,
                                                             step = 1,
                                                             ticks = T
                                                 ),
                                                 
                                                 actionButton('reset_filters', 'Reset Filters',
                                                              icon = icon("sync")
                                                 )
                                    ),
                                    
                                    mainPanel(
                                      plotOutput("plot")
                                    )
                      )
             ),
             
             tabPanel("table",
                      fluidPage(DTOutput('tbl'))
             ))

server <- function(input, output){
  
  output$tbl = renderDT(
    iris, options = list(lengthChange = FALSE), server = F
  )
  
}

shinyApp(ui, server)

Solution

  • plotOutput("plot") was duplicated (see the browser console):

    library(DT)
    library(shiny)
    library(shinyWidgets)
    library(shinydashboard)
    library(datasets)
    
    ui <- navbarPage(
      "App Title",
      # header = css,
      tabPanel(
        "Overview",
        sidebarLayout(
          position = "right",
          fluid = TRUE,
          sidebarPanel = sidebarPanel(
            width = 3,
            radioButtons(
              "percentage_select",
              "Percentage of:",
              c(
                "All Publications" = "p",
                "All SDG Publications" = "l"
              )
            ),
            checkboxGroupInput(
              "sdg_select",
              "SDG:",
              choices = c(1, 2),
              selected = c(1, 2),
              inline = T
            ),
            sliderInput(
              "year_range",
              "Years:",
              min = 2011,
              max = 2020,
              value = range(2011, 2020),
              round = TRUE,
              step = 1,
              ticks = TRUE
            ),
            actionButton('reset_filters', 'Reset Filters',
                         icon = icon("sync"))
          ),
          mainPanel = mainPanel(plotOutput("plot1"))
        )
      ),
      tabPanel(
        "Cumulative",
        sidebarLayout(
          position = "right",
          fluid = TRUE,
          sidebarPanel = sidebarPanel(
            width = 3,
            radioButtons(
              "percentage_select",
              "Indicator:",
              c("Percentage" = "p",
                "Absolute" = "l")
            ),
            checkboxGroupInput(
              "baseline_select",
              "Show average:",
              choices = c("World", "Univ. "),
              selected = c("World", "Univ. "),
              inline = FALSE
            ),
            checkboxGroupInput(
              "sdg_select",
              "SDG:",
              choices = c(1, 2),
              selected = c(1, 2),
              inline = TRUE
            ),
            sliderInput(
              "year_range",
              "Years:",
              min = 2011,
              max = 2020,
              value = range(2011, 2020),
              round = TRUE,
              step = 1,
              ticks = TRUE
            ),
            actionButton('reset_filters', 'Reset Filters',
                         icon = icon("sync"))
          ),
          mainPanel = mainPanel(plotOutput("plot2"))
        )
      ),
      tabPanel("Table", fluidPage(DTOutput('tbl')))
    )
    
    server <- function(input, output, session) {
      output$tbl <- renderDT({
        iris
      },
      options = list(lengthChange = FALSE),
      server = FALSE)
      
    }
    
    shinyApp(ui, server)