如何反应格式化数据 table 列?

How to reactively format data table columns?

在底部的简化功能代码中,在renderDT()下的datatable()colDefs = list()部分,在server部分,我手动center-align使用指令 targets = 1:2, class = "dt-center".

table 的两个 right-most 列输出

我一直在尝试根据在输出中检测到的实际列数 table 使以这种方式格式化的列数具有反应性——因为在完整代码中这是提取出来的from,输出table列的数量根据数据的实际组成而变化。在下面用 # 注释掉的代码中,您可以看到我最近尝试对列进行反应式格式化,当然它不起作用。

请问,如何对 table 列的数据进行反应式格式化,其中 header 行右侧的所有列都被 center-align 编辑?

简化的功能代码:

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)
    )
  })
  
  summed_data <- reactive({
    data() %>%
      group_by(!!sym(input$grouping)) %>% 
        select("ColA","ColB") %>% summarise(across(everything(), sum))
  })
  
  output$data <- renderTable(data())

  output$sums <- renderDT({
    datatable(
      data = summed_data(),
      rownames = FALSE,
      options = 
        list(
            columnDefs = list(
              list(targets = 0, class = "dt-left"),
              list(targets = 1:2, class = "dt-center")
              # list(targets = 1:ncol(summed_data()), class = "dt-center")
              )
        ),
    )
  })
  
}

shinyApp(ui, server)

以下似乎有效。但是,我不确定为什么您的初始方法不起作用 - 它对我来说看起来不错。

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)
    )
  })
  
  summed_data <- reactive({
    data() %>%
      group_by(!!sym(input$grouping)) %>%
      select("ColA", "ColB") %>% summarise(across(everything(), sum))
  })
  
  output$data <- renderTable(data())
  
  output$sums <- renderDT({
    print(names(summed_data())[1])
    datatable(
      data = summed_data(),
      rownames = FALSE,
      options = list(columnDefs = list(
        list(className = 'dt-left', targets = 0),
        list(className = 'dt-center', targets = seq_len(ncol(summed_data())) - 1)
      ))
    )
  })
}

shinyApp(ui, server)