在闪亮的应用程序中使用dt的列宽

时间:2018-05-22 10:45:50

标签: r shiny dt column-width

有没有办法在通过下拉菜单刷新数据后阻止表自行调整大小?

我知道这与R shiny - DT::renderDataTable column width中的问题完全相同,但据我所知,问题从未得到充分回答。

library(shiny)
library(tidyr)
library(dplyr)
library(DT)

ui <- fluidPage(selectInput(inputId = "dayinput", 
                            label = "Day Filter", 
                            choices = c("Monday", "Tuesday","Wednesday")), 
                dataTableOutput("table1")
                )

server <- function(input, output) {

  output$table1 <- renderDataTable({
    price <- c("12", "11", "14")
    day <- c("Monday", "Tuesday", "Wednesday")
    df <- data.frame(price, day) %>% filter(day == input$dayinput)

    datatable(df, rownames = FALSE, class = 'cell-border stripe', 
              options = list(dom = 't', pageLength = -1, lengthMenu = list(c(-1), c('All')),
                             autoWidth = TRUE, columnDefs = list(list(width = '50px', targets = "_all"))))
  })
}

shinyApp(ui =ui, server = server)

1 个答案:

答案 0 :(得分:1)

我认为我第一眼就误解了这个问题。无论如何,我认为你可以通过一些CSS风格实现这一点。

这只是一个小例子:

library(shiny)
library(tidyr)
library(dplyr)
library(DT)

ui <- fluidPage(
  tags$head(
    tags$style(HTML(
      "
#table1 {width: 250px !important}
#DataTables_Table_0 td {width: 125px !important}
      "
    ))
    ),
  selectInput(inputId = "dayinput", 
              label = "Day Filter", 
              choices = c("Monday", "Tuesday","Wednesday","Thuuuuuuuuuuuuuuuursday")), 
  dataTableOutput("table1")
)

server <- function(input, output) {

  output$table1 <- renderDataTable({
    price <- c("12", "11", "14", "15")
    day <- c("Monday", "Tuesday", "Wednesday","Thuuuuuuuuuuuuuuuursday")
    df <- data.frame(price, day) %>% filter(day == input$dayinput)

    datatable(df, rownames = FALSE, class = 'cell-border stripe', 
              options = list(dom = 't', pageLength = -1, lengthMenu = list(c(-1), c('All')),
                             autoWidth = FALSE)
    )
  })
}

shinyApp(ui =ui, server = server)