datatable does not render in Shiny Dashboard

Tim Child picture Tim Child · Jun 30, 2015 · Viewed 10.5k times · Source

The datatable does not render in a Shinydashboard. It just renders a thin white strip for the box. Running only the datatable function in RStudio renders the datatable in the RStudio viewer. So what the correct way to render a DT datatable in a shiny app?

## app.R ##
library(shiny)
library(shinydashboard)
library(htmlwidgets)
library(DT)
library(xtable)
source('../ts01/db.R')

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    fluidRow(
      box(tableOutput("table1"))
    )
  )
)

server <- function(input, output) {
  output$table1 <- DT::renderDataTable({
    datatable(amount_data)
  })  
}

shinyApp(ui, server)

Answer

Pork Chop picture Pork Chop · Jun 30, 2015

You should try the following:

1) tableOutput

rm(list = ls())
library(shiny)
library(shinydashboard)
my_data <- head(mtcars)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    fluidRow(
      box(tableOutput("table1"))
    )
  )
)

server <- function(input, output) {
  output$table1 <- renderTable({
    my_data
  })  
}

shinyApp(ui, server)

2) dataTableOutput

rm(list = ls())
library(shiny)
library(DT)
library(shinydashboard)

my_data <- head(mtcars)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    fluidRow(
      box(DT::dataTableOutput("table1"))
    )
  )
)

server <- function(input, output) {
  output$table1 <- DT::renderDataTable({
    datatable(my_data)
  })  
}

shinyApp(ui, server)