24

I am unable to control the width of a datatable I have added to a shiny app using the function dataTableOutput(). I've tried to use the width parameter within the function but it changes nothing in the output and there is no error ~ it's not telling me that it's ignoring the width parameter.

library(shiny)
library(shinythemes)

ui <- fluidPage(theme = shinytheme("Spacelab"),
            fluidRow(
              column(6,dataTableOutput(outputId = "table")),
              column(6,p(textOutput("para")))
  )
)

server <- function(input, output){

  df <- as.data.frame(matrix(0, ncol = 15, nrow = 20))

  output$table <- renderDataTable({df})

  output$para <- renderText({
    text <- rep(x = "Hello World",1000)
  })
}
shinyApp(ui = ui,server = server)
Collin
  • 440
  • 1
  • 4
  • 11
  • I don't see a `width` argument in `shiny::dataTableOutput`, but it looks like `dataTableOutput` from package DT has one. Have you loaded the DT package after shiny? You can also use `DT::dataTableOutput`. – aosmith Aug 21 '15 at 22:31
  • If I use `DT` instead of `shiny` will the data table still be reactive? – Collin Sep 01 '15 at 04:33
  • try [this SO answer](https://stackoverflow.com/questions/31921238/shrink-dtdatatableoutput-size) – Rivka Jun 29 '17 at 11:39

2 Answers2

42

dataTableOutput does not have an argument width. You can use column within a fluidRow with argument width, supplying an integer between 1 and 12.

library(shinythemes)
ui <- fluidPage(theme = shinytheme("Spacelab"),
    fluidRow(
        column(
            dataTableOutput(outputId = "table"), width = 6)
    )
)

server <- function(input, output){
    df <- as.data.frame(matrix(0, ncol = 20, nrow = 5))
    output$table <- renderDataTable({df}, 
        options = list(scrollX = TRUE))
}
shinyApp(ui = ui,server = server)

Options from the JavaScript library DataTable can be passed directly via renderDataTable argument options. For example, setting scrollX to be true allows tables to scroll.

CSJCampbell
  • 2,025
  • 15
  • 19
  • 3
    I tried using columns and updated my example accordingly, it seems like `dataTableOutput` doesn't want to cooperate – Collin Sep 01 '15 at 04:30
  • 3
    In your original example you had a large number of columns. shiny is still rendering a web page, so if the columns are too wide to fit in a subset of the pane, the table will be displayed using the portion of the page available. – CSJCampbell Sep 02 '15 at 08:53
  • In your updated example, the `textOutput` is displayed in the correct half and the `dataTableOutput` is crashing the text in the Firefox browser. What behaviour would you rather see? – CSJCampbell Sep 02 '15 at 09:08
  • I would like to the see the `dataTableOutput` contained within the specified area. I am thinking you would be able to scroll left and right to view all the columns. I have tried placing the table within an `absolutePanel` but it didn't give the behavior I wanted. Thanks for your help @CSJCampbell – Collin Sep 03 '15 at 17:07
  • 2
    @Collin I have updated the example to demonstrate adding a scrolling bar for the table... you need to pass the JavaScript arguments to `renderDataTable` as a list. – CSJCampbell Sep 21 '15 at 13:51
11

If you use the "DT" R package, and the respective DT::dataTableOutput and DT::renderDataTable, you can use a "width" option with those calls, which apparently can be either a % (e.g. width = "100%") or pixels (width = 300) which should get you the control you want.

See: https://rstudio.github.io/DT/shiny.html

Note from that page:

Important: Be sure to use the DT:: prefix when calling dataTableOutput and renderDataTable so that the DT versions of these functions are guaranteed to be called, instead of the deprecated Shiny versions. If you make sure to library(DT) after library(shiny), normally the DT versions should just override the shiny versions if you do not use the DT:: prefix (when in doubt, use this prefix, until we completely remove these functions from shiny)

user6585640
  • 111
  • 1
  • 2