【问题标题】:How to set independent table column widths using renderTable()?如何使用 renderTable() 设置独立的表格列宽?
【发布时间】: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)

【问题讨论】:

    标签: r shiny format dt


    【解决方案1】:

    您需要先将autoWidth 设置为FALSE,然后您可以使用targets 选项为不同的列指定不同的宽度。第一列的索引为0

    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 = FALSE,
              columnDefs = list(
                list(width = '35px', targets = c(0)),
                list(width = '150px', targets = c(1,2)))
            )
          )
      })
      
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 2015-10-02
      • 2010-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-08
      • 2012-08-13
      • 2020-09-15
      • 1970-01-01
      相关资源
      最近更新 更多