有没有办法防止通过下拉菜单刷新数据后表格自行调整大小?
我知道这与 R shiny - DT::renderDataTable 列宽中的问题完全相同,但据我所知,这个问题从未得到充分回答。
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)
我想我乍一看误解了这个问题。无论如何,我认为你可以通过一些 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)