【发布时间】:2022-01-25 19:08:19
【问题描述】:
在下面的代码中,我正在尝试为渲染的输出表单独调整列宽(“Period...”、“Col A”和“Col B”都可以有自己独特的宽度)在底部的“汇总数据表列:”部分中。此外,一旦用户单击“按周期 2”单选按钮,我使用以下语句 columnDefs = list(list(width = '100px', targets = "_all")) 实现的任何格式都会被清除。
关于如何调整呈现表格中的列宽的任何建议?
当我对此进行研究时,我对此缺乏清晰度/一致性感到惊讶。至少,在我的情况下,能够为“Period ...”调整最左侧列的宽度,并且能够为“Period...”右侧的所有其他列统一调整宽度,这将很有用..",如果无法独立调整每列的宽度。
代码:
library(dplyr)
library(DT)
library(shiny)
library(shinyWidgets)
library(tidyverse)
ui <-
fluidPage(
fluidRow(
column(width = 8,
h3("Data table:"),
tableOutput("data"),
h3("Sum the data table columns:"),
radioButtons(
inputId = "grouping",
label = NULL,
choiceNames = c("By period 1", "By period 2"),
choiceValues = c("Period_1", "Period_2"),
selected = "Period_1",
inline = TRUE
),
DT::dataTableOutput("sums")
)
)
)
server <- function(input, output, session) {
data <- reactive({
data.frame(
Period_1 = c("2020-01", "2020-02", "2020-03", "2020-01", "2020-02", "2020-03"),
Period_2 = c(1, 2, 3, 3, 1, 2),
ColA = c(1000.01, 20, 30, 40, 50, 60),
ColB = c(15.06, 25, 35, 45, 55, 65)
)
})
colNames <- reactive({c(stringr::str_replace(input$grouping, fixed("_"), " "), "Col A", "Col B") })
summed_data <- reactive({
data() %>%
group_by(!!sym(input$grouping)) %>%
select("ColA","ColB") %>%
summarise(across(everything(), sum))
})
output$data <- renderTable(data())
output$sums <- renderDT({
summed_data() %>%
datatable(
rownames = FALSE,
colnames=colNames(),
options = list(
autoWidth = TRUE,
columnDefs = list(list(width = '100px', targets = "_all"))
)
)
})
}
shinyApp(ui, server)
【问题讨论】: