简体   繁体   English

R DT :: datatables同时格式化多列

[英]R DT::datatables formatting multiple columns simultaneously

I wish to implement formatCurrency() and formatPercentage() (both from DT package) across multiple columns simultaneously in a shiny dashboard. 我希望在一个闪亮的仪表板上同时跨多个列实现formatCurrency()formatPercentage() (均来自DT包)。 I am using shinymaterial for the given example. 对于给定的示例,我正在使用Shinymaterial。

I am currently doing the following: 我目前正在执行以下操作:

# 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)

However, what I wish to do is, within the renderDataTable() function, to simplify the format functions into fewer lines. 但是,我想做的是在renderDataTable()函数中,将格式函数简化为更少的行。 For example, implement formatCurrency() in any column with a "$" and formatPercentage() in any column with a "%". 例如,实施formatCurrency()中的任何一列以“$”和formatPercentage()中的任何一列用“%”。

I have done a fair bit of searching for an appropriate but could not find a solution, but I assume I am just missing a fairly simple solution. 我已经做了相当多的寻找适当的解决方案,但是找不到解决方案,但是我想我只是缺少一个相当简单的解决方案。

Something like: 就像是:

# 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)
  })

A few additional points: 其他几点:

  • The tibble will actually be a reactive 小事实际上是一种反应
  • This example is a very trivial version of a rather more complex table and set of reactives 这个例子是一个相当复杂的表和一组反应式的非常琐碎的版本
  • I do not want to implement the formatting in the reactive part since I find this then messes with the DT sorting function, since it assumes the column is a character string 我不想在反应部分中实现格式设置,因为我发现这会与DT排序功能混淆,因为它假定列是字符串

Any help will be greatly appreciated 任何帮助将不胜感激

Try: 尝试:

# 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)
  })

It seems you need to be explicit with the data so colnames() doesn't work - you need colnames(data_table_example_data) . 似乎您需要对数据进行明确显示,因此colnames()不起作用-您需要colnames(data_table_example_data)

I noticed during testing if you use grepl with rownames = TRUE that rownames becomes the first column name which means all the formatting is out by one. 我在测试过程中注意到,如果您将greplgrepl rownames = TRUE一起使用,则grepl会成为第一列名称,这意味着所有格式都被一一排除。 grep seems to not have this issue. grep似乎没有这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM