3

我目前正在使用flexdashboardand创建一个小型仪表板shiny。它包含许多会计列,其中必须能够对值进行排序。作为表格的一部分,我使用库在渲染表格之前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)
  )

```

下面的小图说明了我排序降序时的问题:

例子

4

0 回答 0