【问题标题】:R DT::datatables formatting multiple columns simultaneouslyR DT::datatables 同时格式化多列
【发布时间】:2019-04-12 22:11:54
【问题描述】:

我希望在闪亮的仪表板中同时跨多个列实现 formatCurrency()formatPercentage()(均来自 DT 包)。我在给定的例子中使用了闪亮的材料。

我目前正在做以下事情:

# The packages to load.
required_packages <- c("shiny", "shinymaterial", "DT", "tidyverse")

# This function will load in all the packages needed.
lapply(required_packages, require, character.only = TRUE)

# A table example.
ui <- material_page(
  title = "Example table",
  tags$h1("Table example"),
  material_card(
    title = "Table",
    material_row(
      DT::dataTableOutput("data_table_example")
    ),
    depth = 1
  )
)

server <- function(input, output) {

  data_table_example_data = tibble(
    Person = paste0("Person ", c(1:100)),
    `Price $` = rnorm(100, 50000, 500),
    `Cost $` = rnorm(100, 30000, 300),
    `Probability %` = rnorm(100, 0.6, 0.1),
    `Win %` = rnorm(100, 0.5, 0.2)
    )

  # This will create an output summary table
  output$data_table_example = renderDataTable({
    result = datatable(data_table_example_data, options = list(pageLength = 100, scrollX = TRUE), 
                       class = 'cell-border stripe compact', rownames = FALSE) %>%
      formatCurrency("Price $") %>%
      formatCurrency("Cost $") %>%
      formatPercentage("Probability %", digits = 1) %>%
      formatPercentage("Win %", digits = 1)
  })
}

shinyApp(ui = ui, server = server)

但是,我希望在 renderDataTable() 函数中将格式函数简化为更少的行。例如,在任何带有“$”的列中实现formatCurrency(),在任何带有“%”的列中实现formatPercentage()

我已经进行了相当多的搜索以寻找合适的但找不到解决方案,但我想我只是缺少一个相当简单的解决方案。

类似:

# This will create an output summary table
  output$data_table_example = renderDataTable({
    result = datatable(data_table_example_data, options = list(pageLength = 100, scrollX = TRUE), 
                       class = 'cell-border stripe compact', rownames = FALSE) %>%
      formatCurrency(grepl("$", colnames()) %>%
      formatPercentage(grepl("%", colnames()), digits = 1)
  })

补充几点:

  • tibble 实际上是一个响应式
  • 这个例子是一个相当复杂的表和一组反应式的非常简单的版本
  • 我不想在反应部分中实现格式化,因为我发现这会与 DT 排序函数混淆,因为它假定列是字符串

任何帮助将不胜感激

【问题讨论】:

    标签: r shiny shinydashboard dt


    【解决方案1】:

    试试:

    # This will create an output summary table
      output$data_table_example = renderDataTable({
        result = datatable(data_table_example_data, options = list(pageLength = 100, scrollX = TRUE), 
                           class = 'cell-border stripe compact', rownames = FALSE) %>%
          formatCurrency(grepl("$", colnames(data_table_example_data)) %>%
          formatPercentage(grepl("%", colnames(data_table_example_data)), digits = 1)
      })
    

    看来您需要明确说明数据,因此 colnames() 不起作用 - 您需要 colnames(data_table_example_data)

    我在测试过程中注意到,如果您将greplrownames = TRUE 一起使用,那么rownames 将成为第一个列名,这意味着所有格式都被淘汰了。 grep 好像没有这个问题。

    【讨论】:

      猜你喜欢
      • 2018-04-13
      • 2017-11-26
      • 1970-01-01
      • 2019-05-16
      • 2018-07-31
      • 1970-01-01
      • 1970-01-01
      • 2018-08-25
      • 2021-12-13
      相关资源
      最近更新 更多