Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controlling table width in Shiny dataTableOutput

Tags:

r

shiny

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)
like image 495
Collin Avatar asked Aug 21 '15 21:08

Collin


2 Answers

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.

like image 188
CSJCampbell Avatar answered Feb 01 '23 03:02

CSJCampbell


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)

like image 22
user6585640 Avatar answered Feb 01 '23 02:02

user6585640