14
votes

I'm building an R shiny dashboard and when I put my data in a table using the DT package and renderdatatable(). At the top of each column, I have filters, the search box is too narrow to see the text and select an option. Here's an image:

enter image description here

Does anyone know of a way to increase the width?

Here's my code for the datatable code in the server.r:

  output$table <- DT::renderDataTable(DT::datatable({    
    data <- rv$data
    if (input$sour != "All") {
      data <- data[data[,1] == input$sour,]
    }else{data}
    if (input$sour1 != "All") {
      data <-data[data[,2] == input$sour1,]
    }else{data}
    if (input$tran != "All") {
      data <-data[data[,3] == input$tran,]
    }else{data}
  },filter='top'))

here's the code in the ui.r:

 tabItem(tabName = "ResultsTable",
              fluidPage(  
                headerPanel(
                  h1("List", align="center",  style = "font-family: 'Verdana';font-weight: 800; line-height: 1.1;   color: #151515;")),
                # fluidRow(
                #     column(8, DT::dataTableOutput("table",width = "100%"),offset = 2)))),
                #                 # Create a new Row in the UI for selectInputs
                fluidRow(

                  column(4,
                         selectInput("sour",
                                     "Name:",
                                     c("All",
                                       unique(as.character(df[,1]))))
                  ),
                  column(4,
                         selectInput("sour1",
                                     "Number:",
                                     c("All",
                                       unique(as.character(df[,2]))))
                  ),
                  column(4,
                         selectInput("tran",
                                     "Code:",
                                     c("All",
                                       unique(as.character(df[,3])))))),
                # Create a new row for the table.
                fluidRow(column(11, DT::dataTableOutput("table",width = "95%")))))

I tried this, but it did not work:

 output$table <- DT::renderDataTable(DT::datatable({    
    data <- rv$data
    if (input$sour != "All") {
      data <- data[data[,1] == input$sour,]
    }else{data}
    if (input$sour1 != "All") {
      data <-data[data[,2] == input$sour1,]
    }else{data}
    if (input$tran != "All") {
      data <-data[data[,3] == input$tran,]
    }else{data}
  },filter='top',options = list(
    autoWidth = TRUE,
    columnDefs = list(list(width = '200px', targets = "_all"))
  )))
3
A hackish way is to rename "Date List" column to be a long string without spaces, e.g. to "DateList" or "Date_List". The shiny app will make the drop down at least as wide as that string. - Sergei

3 Answers

7
votes

I solved this problem using CSS:

input {
    width: 100px !important;
}

You can also apply this style to factor filters only:

td[data-type="factor"] input {
    width: 100px !important;
}

Put my.css file in www subdirectory, and link to it:

shinyApp(
    ui = fluidPage(
        tags$head(
            tags$link(
                rel = "stylesheet",
                type = "text/css",
                href = "my.css")
        ),

        DT::dataTableOutput(...)
    )
4
votes

A similar question was answered here.

Also, in order to use range sliders to filter rows within ranges, consider converting the "Date List" column to the date format using as.Date().

-1
votes

Yihui's solution here seems to work very well: https://github.com/rstudio/DT/issues/172

datatable(..., options = list(autoWidth = TRUE))