打印像一个字符,但排序像数字在Shiny和DataTable [英] Printing like a character but sorting like numeric in Shiny and DataTable

查看:113
本文介绍了打印像一个字符,但排序像数字在Shiny和DataTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想排序以美元格式化的DataTable列(因此是一个字符)。我已经使用 scales :: dollar()进行格式化。这将字段转换为导致排序问题的字符(例如,$ 8>$ 10)。

I would like to sort a DataTable column that is formatted with dollars (and thus is a character). I have used scales::dollar() for formatting. This converts the field to a character which causes sorting problems (for instance, "$8" > "$10").

如何将字段排序为数字?或者,我可以保持字段为数字,只是用美元格式打印?

How can I sort the field as if it were numeric? Alternatively, can I keep the field as numeric and just print with dollar formatting?

app.R (需要Shiny 0.10.2) p>

app.R (requires Shiny 0.10.2)

server <- function(input, output) {
  output$foo_table <- renderDataTable({
    x <- seq(8000, 12000, by = 1000)
    x <- scales::dollar(x)
    d <- data.frame(x, stringsAsFactors = FALSE)
    d
  })
}

ui <- shinyUI(fluidPage(
    mainPanel(dataTableOutput("foo_table"))
  )
)

shinyApp(ui = ui, server = server)


推荐答案

稍晚,但 DT Package 现在具有格式化功能,包括formatCurrency:

A bit late, but the DT Package now has format functions, including formatCurrency:

# format the columns A and C as currency, and D as percentages
datatable(m) %>% formatCurrency(c('A', 'C')) %>% formatPercentage('D', 2)

从函数页面:


这些格式化函数只是包含rowCallback选项的包装生成适当的JavaScript代码。
类似地,可以使用formatDate()函数来格式化日期/时间列。它有一个方法参数,从可能的转换方法的列表中获取值:toDateString,toISOString,toLocaleDateString,toLocaleString,toLocaleTimeString,toString,toTimeString,toUTCString。

Under the hood, these formatting functions are just wrappers for the rowCallback option to generate the appropriate JavaScript code. Similarly, there is a formatDate() function that can be used to format date/time columns. It has a method argument that takes values from a list of possible conversion methods: toDateString, toISOString, toLocaleDateString, toLocaleString, toLocaleTimeString, toString, toTimeString, toUTCString.

这篇关于打印像一个字符,但排序像数字在Shiny和DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆