2

I am trying to change width of column in DT::datatable, but I am having no luck. Based on the code here, specifying column width in columnDefs should be straight forward.

However I am unable to get my columns to change width:

library(shiny)
library(DT)

ui <- fluidPage(mainPanel(DT::dataTableOutput("test1"),
                          DT::dataTableOutput("test2"),
                          DT::dataTableOutput("test3")))

server <- function(input, output) {
  x <- structure(list(respondent = c(1847291, 1823441, 1932184, 1832521
  ), answer = c("Persia", "US", "Romainia", "Guiana"), SimilarResponse = c(NA,
                                                                           "USA", "Romania", "Guyana"), SimilarResponseUpcode = c(NA, 1,
                                                                                                                                  4, 2)), row.names = c(NA, -4L), .Names = c("respondent", "answer",
                                                                                                                                                                             "SimilarResponse", "SimilarResponseUpcode"), class = "data.frame")

  output$test1 <- DT::renderDataTable(
    x
    ,
    rownames = FALSE,
    selection = list(mode = "none"),
    options = list(autoWidth = TRUE,
                   columnDefs = list(list(
                     width = '100px', target = 3
                   )))
  )

  output$test2 <- DT::renderDataTable(
    x
    ,
    rownames = FALSE,
    selection = list(mode = "none"),
    options = list(autoWidth = TRUE,
                   columnDefs = list(list(
                     width = '100px', targets = 3
                   )))
  )

  output$test3 <- DT::renderDataTable(
    x
    ,
    rownames = FALSE,
    selection = list(mode = "none"),
    options = list(autoWidth = TRUE,
                   columnDefs = list(list(
                     width = '20px', targets = 0:3
                   )))
  )


}

shinyApp(ui = ui, server = server)

All three tables fail to change width size. When I run the examples from DT width changes, so I am confused as to what I am doing wrong.

Carl
  • 5,569
  • 6
  • 39
  • 74

0 Answers0