在 DT 中舍入货币格式的数字

epo*_*po3 6 formatting r dt

我正在尝试以货币格式获取数字,然后对它们进行四舍五入,但是我从DT(v 0.1) 中得到了意外的行为。

我想要的值看起来像808084.227872401£808,084.2

这是代码:

library(DT)

m <- structure(list(A = c(808084.227872401, 1968554.9592654, 751271.053745238, 
-248530.769710688, 1022891.09543523, -407303.626363765), B = c(143073.342325492, 
-1440469.87343229, -590080.736184761, -608299.78907882, 1167155.65688074, 
803870.898483576), C = c(-447086.9382469, 606572.488852836, 89371.3745637198, 
-1496047.6143101, -410103.544644035, 1106358.3287006), D = c(0.754009573487565, 
0.364774209912866, 0.525769896339625, 0.44853704655543, 0.909551323624328, 
0.439131782157347), E = c(98.8604132297185, 98.9055931760521, 
99.3795062166865, 98.5895350315005, 101.194549174315, 102.325111315431
)), .Names = c("A", "B", "C", "D", "E"), row.names = c(NA, -6L
), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)

根据文档,这应该有效:

datatable(m) %>% formatCurrency("A", "£", digits = 1)
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误:

格式错误(。,“A”,“£”,数字 = 1):未使用的参数(数字 = 1)

然后我尝试了另一个命令:

datatable(m) %>% formatCurrency("A", "£") %>% formatRound("A", 1)
Run Code Online (Sandbox Code Playgroud)

但它只格式化了货币而不对其进行四舍五入。

在此处输入图片说明

有任何想法吗?

附注。我知道这个答案,但我不想显示字符串,因为我想在显示时对数字进行排序datatable

lrn*_*cig 5

我的结论是你不能用 将 2 个格式化程序添加到同一列DT,当然我可能是错的。

请注意,即使在 的文档中没有明确说明这一点DT,每个列表也只能添加一个格式化程序。另请注意,在您提供的链接中的示例中,或者当您键入时?formatCurrency,当它们包含两个管道时%>%,它们总是会影响两个不同的列。

在你的例子中,当你做

datatable(m) %>% formatRound("A", digits=1) %>% formatCurrency("A", currency="£")
Run Code Online (Sandbox Code Playgroud)

结果是四舍五入的 1 位数字,没有货币,如果你这样做

datatable(m) %>% formatCurrency("A", currency="£") %>% formatRound("A", digits=1)
Run Code Online (Sandbox Code Playgroud)

结果是添加的货币没有四舍五入。

我对 R 如何与 js 集成的了解非常有限,但是查看cran 中包的 R 源,看起来管道中的每个格式命令都附加了一个格式化程序,但由于某种原因,只有一个格式化程序起作用:

formatCurrency = function(table, columns, currency = '$', interval = 3, mark = ',') {
  formatColumns(table, columns, tplCurrency, currency, interval, mark)
}

formatRound = function(table, columns, digits = 2) {
  formatColumns(table, columns, tplRound, digits)
}

formatColumns = function(table, columns, template, ...) {
  ...
  x$options$rowCallback = appendFormatter(
    x$options$rowCallback, columns, colnames, rownames, template, ...
  )
  ...
}

appendFormatter = function(js, name, names, rownames = TRUE, template, ...) {
  ...
  JS(append(
    js, after = 1,
    template(i, ...)
  ))
}
Run Code Online (Sandbox Code Playgroud)

每个格式化程序最终formatColumns以不同的调用template,并i解析列的 id。正如我所说,我不知道这是因为追加操作覆盖了格式化程序,还是与执行有关。


编辑:对不起,我不小心按下了发布按钮并被打扰了。我实际上实现了一个需要更多参数的格式化程序。解决方案有点复杂,但它有效。这是一个接受货币和数字的格式化程序:

tplRound2 = function(cols, currency, digits) {
  sprintf(
    "var d = parseFloat(data[%d]); $(this.api().cell(row, %s).node()).html(isNaN(d) ? '' : '%s' + d.toFixed(%d).toString());",
    cols, cols, currency, digits
  )
}
Run Code Online (Sandbox Code Playgroud)

您需要将所有这些功能添加到您的会话中:

formatRound2 = function(table, columns, currency, digits = 2) {
  formatColumns2(table, columns, tplRound2, currency, digits)
}

formatColumns2 = function(table, columns, template, ...) {
  if (inherits(columns, 'formula')) columns = all.vars(columns)
  x = table$x
  colnames = base::attr(x, 'colnames', exact = TRUE)
  rownames = base::attr(x, 'rownames', exact = TRUE)
  x$options$rowCallback = appendFormatter2(
    x$options$rowCallback, columns, colnames, rownames, template, ...
  )
  table$x = x
  table
}

name2int = function(name, names) {
  if (is.numeric(name)) {
    return(if (all(name > 0)) name else seq_along(names)[name])
  }
  names = setNames(seq_along(names), names)
  unname(names[name])
}

appendFormatter2 = function(js, name, names, rownames = TRUE, template, ...) {
  js = if (length(js) == 0) c('function(row, data) {', '}') else {
    unlist(strsplit(as.character(js), '\n'))
  }
  i = name2int(name, names)
  if (is.character(name) || (is.numeric(name) && !rownames)) i = i - 1
  if (any(is.na(i))) stop(
    'You specified the columns: ', paste(name, collapse = ', '), ', ',
    'but the column names of the data are ', paste(names, collapse = ', ')
  )
  JS(append(
    js, after = 1,
    template(i, ...)
  ))
}
Run Code Online (Sandbox Code Playgroud)

然后您可以使用新的格式化程序运行以获得所需的结果:

datatable(m) %>% formatRound2("A", "£", digits=1)
Run Code Online (Sandbox Code Playgroud)

(但是,这不会添加,每 3 位数字,如果您确实需要它,我可以将其添加到格式化程序中...)


评论后的 EDIT2

这将是使用货币和位数的格式化程序功能,加上 ',' 标记:

tplRound3 = function(cols, currency, digits, interval, mark) {
  sprintf(
    "var d = parseFloat(data[%d]); $(this.api().cell(row, %s).node()).html(isNaN(d) ? '' : '%s' + d.toFixed(%d).toString().replace(/\\B(?=(\\d{%d})+(?!\\d))/g, '%s'));",
    cols, cols, currency, digits, interval, mark
  )
}


formatRound3 = function(table, columns, currency, digits = 2, interval=3, mark=',') {
  formatColumns2(table, columns, tplRound3, currency, digits, interval, mark)
}
Run Code Online (Sandbox Code Playgroud)

为了使用它,只需键入

datatable(m) %>% formatRound3("A", "£", digits=1)
Run Code Online (Sandbox Code Playgroud)