Formattable打破了Shiny中DataTable中的列排序

时间:2017-07-12 12:43:27

标签: r datatables flexdashboard formattable

我目前正在使用flexdashboardshiny创建一个小仪表板。它包含许多会计列,其中必须能够对值进行排序。作为表的一部分,我在呈现表之前使用formattable库将列格式化为accounting

问题是格式化的列会破坏DataTable的排序,因为formattable实际上将数字转换为文本。

以下是重现此问题的代码:

---
title: "Dashboard"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: scroll
runtime: shiny
---

```{r packages, include = F}
library(DT)
library(formattable)
library(dplyr)
```

```{r}
sign_formatter <- formattable::formatter("span", 
                                         style = x ~ style(color = ifelse(x > 0, "green", 
                                                                          ifelse(x < 0, "red", "black"))))
```

Results {data-height=800 data-width=100%}
---------------------------------------------

```{r data}
df <- data.frame(category = sample(LETTERS, 30, replace = T), 
                 income = runif(30, -10, 10) * 1e4)

# Format Table
df <- df %>%
  mutate(`income` = accounting(`income`, digits = 0L))

renderDataTable(
  formattable(df, list(
     `income` = sign_formatter
    )) %>%
    as.datatable(., class = 'cell-border stripe table table-striped',
                 filter = 'top', escape = FALSE, selection = 'none',
                 options = list(autoWidth = TRUE,
                                server = TRUE,
                                fixedHeader = TRUE,
                                columnDefs = list(list(width = '200px', targets = c(1)))
                                ), rownames = FALSE)
  )

```

下面的小图说明了我对降序进行排序的问题:

example

0 个答案:

没有答案