r dt(dataTable)中货币,数字和百分比的条件(行)形成(划分)

发布于 2025-02-13 09:33:05 字数 943 浏览 0 评论 0原文

我的DT输出(微光)中有一个具有数值的列,该数字值依赖于另一列。有些值是百分比,有些是货币,有些是普通数字。

例如,我想转到此输入...

DefaultFormat
PCT12345.67
美元12345.67
编号12345.67

...进入此DT输出:

DefaultFormatValue
PCT123.45
$
编号12,345.67

12,345 格式化()格式化()函数可以完成这些相应格式中的我所需的工作,但它们会影响整个列而不是特定的单元格。另一方面,formatstyle()可以基于另一列的特定单元格在列中定位特定的单元格,但我无法找到一种使其更改内容而不是样式的方法。

此外,我尝试使用formatStyle()尝试设置类,希望在.css文件中我可以定位目标,例如.pctClass:.currencyClass。 :之前,但它忽略了类属性。

获得formatStyle()的有条件行为的好方法是什么?

I have column in my DT output (in Shiny) that has a numeric value whose units depend on another column. Some values are percentages, some are currency, and some are plain numbers.

For example, I would like to turn this input...

DefaultFormatValue
PCT12345.67
DOLLAR12345.67
NUMBER12345.67

...into this DT output:

DefaultFormatValue
PCT123.45%
DOLLAR$12,345
NUMBER12,345.67

The formatCurrency(), formatPercentage() and formatRound() functions do what I need for each of these respective formats but they affect the entire column instead specific cells. On the other hand formatStyle() can target specific cells in a column based on another column but I can't figure out a way to have it change the contents rather than the styles.

Furthermore, I tried setting the class using formatStyle() in the hopes that in the .css file I could then target, e.g. .pctclass:after and .currencyclass:before but it ignores the class attribute.

What is a good way to get the conditional behavior of formatStyle() but for numbers, percentages, and currencies?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情释 2025-02-20 09:33:05

编辑:这是从此处借用的解决方案: https://stackoverflow.com/a/35657820/6851825

试图根据基础数据对格式化的列进行分类,而不是其多样的格式化外观。您可以通过使用未格式的助手列来处理排序来执行此操作:

library(dplyr)
data.frame(
  stringsAsFactors = FALSE,
  DefaultFormat = c("PCT", "DOLLAR", "NUMBER"),
  Value = c(54.54, 12345.67, 12345.67)
) %>%
  mutate(Value_fmt = case_when(DefaultFormat == "PCT" ~ scales::percent(Value),
                           DefaultFormat == "DOLLAR" ~ scales::dollar(Value),
                           DefaultFormat == "NUMBER" ~ scales::comma(Value),
                           TRUE ~ as.character(Value)) %>%
           forcats::fct_reorder(Value), .after = 1) %>%
  
  DT::datatable(rownames = FALSE, options = list(columnDefs = list(
    list(orderData = 2, targets = 1),
    list(visible=FALSE, targets = 2))))

例如,请注意5 454%即使在其他条目之前出现在其他条目之前,即使以后是字母表:

“在此处输入图像说明”

EDIT: here's a solution borrowing from the approach here: https://stackoverflow.com/a/35657820/6851825

You are seeking to sort a formatted column based on the underlying data instead of its varied formatted appearance. You can do this by using an unformatted helper column to handle the sorting:

library(dplyr)
data.frame(
  stringsAsFactors = FALSE,
  DefaultFormat = c("PCT", "DOLLAR", "NUMBER"),
  Value = c(54.54, 12345.67, 12345.67)
) %>%
  mutate(Value_fmt = case_when(DefaultFormat == "PCT" ~ scales::percent(Value),
                           DefaultFormat == "DOLLAR" ~ scales::dollar(Value),
                           DefaultFormat == "NUMBER" ~ scales::comma(Value),
                           TRUE ~ as.character(Value)) %>%
           forcats::fct_reorder(Value), .after = 1) %>%
  
  DT::datatable(rownames = FALSE, options = list(columnDefs = list(
    list(orderData = 2, targets = 1),
    list(visible=FALSE, targets = 2))))

For example, note how 5 454% appears before the other entries even though it is alphabetically later:

enter image description here

玩心态 2025-02-20 09:33:05

(这不是dt - 特定的,目前尚不清楚这是否是必要的。)

您可以分组或拆分并分配:

library(dplyr)
set.seed(2)
dat <- data.frame(fmt = sample(c("PCT","DOLLAR","NUMBER"), 10, replace = TRUE), value = round(runif(10, 10, 9999), 2))

dat %>%
  group_by(fmt) %>%
  mutate(value2 = switch(fmt[1],
    PCT=scales::percent(value),
    DOLLAR=scales::dollar(value),
    NUMBER=scales::percent(value),
    as.character(value))
  )
# # A tibble: 10 x 3
# # Groups:   fmt [3]
#    fmt    value value2   
#    <chr>  <dbl> <chr>    
#  1 PCT    1816. 181 621% 
#  2 NUMBER 4058. 405 836% 
#  3 DOLLAR 8536. $8,536.10
#  4 DOLLAR 9763. $9,763.24
#  5 PCT    2266. 226 577% 
#  6 PCT    4453. 445 320% 
#  7 PCT     759. 75 897%  
#  8 PCT    6622. 662 171% 
#  9 PCT    3881. 388 123% 
# 10 DOLLAR 8370. $8,369.69

替代方案是使用case_when和它会产生非常相似的结果,但一次将工作一个字符串。此方法调用格式函数一次,每组 ,也许更有效。 (如果有必要,请给您。)

(This is not DT-specific, it wasn't clear if that was a requirement.)

You can group or split and assign:

library(dplyr)
set.seed(2)
dat <- data.frame(fmt = sample(c("PCT","DOLLAR","NUMBER"), 10, replace = TRUE), value = round(runif(10, 10, 9999), 2))

dat %>%
  group_by(fmt) %>%
  mutate(value2 = switch(fmt[1],
    PCT=scales::percent(value),
    DOLLAR=scales::dollar(value),
    NUMBER=scales::percent(value),
    as.character(value))
  )
# # A tibble: 10 x 3
# # Groups:   fmt [3]
#    fmt    value value2   
#    <chr>  <dbl> <chr>    
#  1 PCT    1816. 181 621% 
#  2 NUMBER 4058. 405 836% 
#  3 DOLLAR 8536. $8,536.10
#  4 DOLLAR 9763. $9,763.24
#  5 PCT    2266. 226 577% 
#  6 PCT    4453. 445 320% 
#  7 PCT     759. 75 897%  
#  8 PCT    6622. 662 171% 
#  9 PCT    3881. 388 123% 
# 10 DOLLAR 8370. $8,369.69

An alternative would be to use case_when and it would come up with very similar results, but it will be working one string at a time; this method calls the format function once per group, perhaps a bit more efficient. (Over to you if that's necessary.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文