R闪亮的数据表过滤器框大小缩小以查看文本

时间:2016-03-22 20:23:11

标签: r filter datatable shiny dt

我正在构建一个R闪亮的仪表板,当我使用DT包和renderdatatable()将数据放入表中时。在每列的顶部,我有过滤器,搜索框太窄,无法查看文本并选择一个选项。这是一张图片:

enter image description here

有谁知道增加宽度的方法?

这是我在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'))

这里是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%")))))

我尝试了这个,但它不起作用:

 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 个答案:

答案 0 :(得分:3)

我使用CSS解决了这个问题:

input {
    width: 100px !important;
}

您也可以将此款式仅应用于factor过滤器:

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

my.css文件放在www子目录中,并链接到它:

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

        DT::dataTableOutput(...)
    )

答案 1 :(得分:2)

A similar question was answered here.

另外,按顺序to use range sliders to filter rows within ranges,考虑转换&#34;日期列表&#34;使用as.Date()列到日期格式。

答案 2 :(得分:0)

Yihui在这里的解决方案似乎效果很好: https://github.com/rstudio/DT/issues/172

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