防止数据 table 列宽改变大小 Shiny
Prevent data table column width from changing size Shiny
我有一个数据 table,其右列宽度在每次单击“添加”按钮时都会更新。添加额外的行时,它会自动调整大小以适合整个主面板,尽管文本很适合。
这是我的 REPREX:
library(shiny)
library(DT)
this_table = data.frame(bins = c(30, 50), cb = c(T, F))
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30),
checkboxInput("cb", "T/F"),
actionButton("add_btn", "Add")
),
mainPanel(
DTOutput("shiny_table")
)
)
)
server <- function(input, output) {
this_table <- reactiveVal(this_table)
observeEvent(input$add_btn, {
t = rbind(data.frame(bins = input$bins,
cb = input$cb), this_table())
this_table(t)
})
output$shiny_table <- renderDT({
datatable(this_table(),
rownames = FALSE,
options = list(
autoWidth = TRUE,
columnDefs = list(list(width = "40%", targets = "_all"), list(className = "dt-center", targets = "_all")),
pageLength = 23, info = FALSE, lengthMenu = 30,
paging = FALSE,
#searching = FALSE,
lengthChange = FALSE,
ordering = FALSE ))
})
}
shinyApp(ui = ui, server = server)
如何防止这种情况发生,从而使列宽保持不变?
这很奇怪。看起来 width
选项在添加行时消失了。您可以将 DTOutput
包裹在具有给定宽度的 div
元素中,并设置 margin: auto
使其居中:
mainPanel(
div(
style = "width: 300px; margin: auto;",
DTOutput("shiny_table")
)
)
我有一个数据 table,其右列宽度在每次单击“添加”按钮时都会更新。添加额外的行时,它会自动调整大小以适合整个主面板,尽管文本很适合。
这是我的 REPREX:
library(shiny)
library(DT)
this_table = data.frame(bins = c(30, 50), cb = c(T, F))
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30),
checkboxInput("cb", "T/F"),
actionButton("add_btn", "Add")
),
mainPanel(
DTOutput("shiny_table")
)
)
)
server <- function(input, output) {
this_table <- reactiveVal(this_table)
observeEvent(input$add_btn, {
t = rbind(data.frame(bins = input$bins,
cb = input$cb), this_table())
this_table(t)
})
output$shiny_table <- renderDT({
datatable(this_table(),
rownames = FALSE,
options = list(
autoWidth = TRUE,
columnDefs = list(list(width = "40%", targets = "_all"), list(className = "dt-center", targets = "_all")),
pageLength = 23, info = FALSE, lengthMenu = 30,
paging = FALSE,
#searching = FALSE,
lengthChange = FALSE,
ordering = FALSE ))
})
}
shinyApp(ui = ui, server = server)
如何防止这种情况发生,从而使列宽保持不变?
这很奇怪。看起来 width
选项在添加行时消失了。您可以将 DTOutput
包裹在具有给定宽度的 div
元素中,并设置 margin: auto
使其居中:
mainPanel(
div(
style = "width: 300px; margin: auto;",
DTOutput("shiny_table")
)
)