8

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)
Tim Child
  • 2,994
  • 1
  • 26
  • 25
  • As you havent provided the actual table content, my initial guess is that you change source('../ts01/db.R') to source('../ts01/db.R', local=TRUE). – Pork Chop Jun 30 '15 at 06:59
  • Also not sure it necessary to call `datatable(amount_data)`, I think if amount_data is a `data.frame` it will do so without the extra command. – Brandon Bertelsen Jun 30 '15 at 07:00
  • The data comes via source('../ts01/db.R'). This R script uses RMySQl to query a SQL table into a valid data frame. – Tim Child Jun 30 '15 at 12:29
  • In RStudio the a call to datatable(amount_data) renders the datatable in the RStudio viewer. Let's assume it's required for the rendering the data. – Tim Child Jun 30 '15 at 12:32
  • 1
    `renderDataTable()` must be paired with `dataTableOutput()` instead of `tableOutput()` – Yihui Xie Jul 04 '15 at 04:12

2 Answers2

12

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)
Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
Pork Chop
  • 28,528
  • 5
  • 63
  • 77
  • 4
    if you shrink the browser window you will notice that the datatable isn't contained inside the box element. use datatable(my_data, extensions = 'Responsive') – MySchizoBuddy Oct 09 '15 at 17:43
6

To make sure you use the right package to render your datatable use this in your ui instead:

DT::dataTableOutput('table1')
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
JDH
  • 165
  • 2
  • 8