column width using DT datatable

5,269 views
Skip to first unread message

HH

unread,
Feb 9, 2016, 1:43:06 PM2/9/16
to Shiny - Web Framework for R
Hello, I have a dumb question about how to set individual column width for a datatable.
Here's my sample code:
library(shiny)
library(DT)
runApp(list(
  ui = navbarPage(
    title = 'Tables',
    tabPanel('test', 
             numericInput('nmb', label="change number of columns", value=2,min=1,max=10,step=1)
             ,
             DT::dataTableOutput('table1')
            
             
    )),
  server = function(input, output) {
   
   Tab <- reactive({
     n <- input$nmb
     return(iris[,1:n])
     })
    output$table1 <- DT::renderDataTable({
    datatable(Tab(),rownames=FALSE)
      
    })
  }
))

Actually, I'm trying to display a dataframe whose number of columns depends on a user-input number. 
Is there a nice way to define different column width individually from the server side? For example, 200px for 1st col, 300px for 2nd, etc.
Is it possible to let the user define them via an Input widget in UI? 
I'm no sure if I describe my question clearly. THANKS!

Phill Clarke

unread,
Feb 12, 2016, 4:33:50 AM2/12/16
to Shiny - Web Framework for R
Perhaps columnDefs will assist:


datatable(..., options = list(
  autoWidth = TRUE,
  columnDefs = list(list(width = '200px', targets = c(1, 3)))
))

I think it hopefully wouldn't be a big step to then set the targets as user input values.
Reply all
Reply to author
Forward
0 new messages