【发布时间】: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