Formattable中断数据表中的列排序
我目前正在使用Formattable中断数据表中的列排序,r,datatables,flexdashboard,formattable,R,Datatables,Flexdashboard,Formattable,我目前正在使用flexdashboard和Shining创建一个小仪表板。它包含许多会计列,在这些列中,能够对值进行排序至关重要。作为表的一部分,在呈现表之前,我使用formattable库将列格式化为accounting 问题在于,格式化列破坏了DataTable的排序,因为formattable实质上是将数字转换为文本 以下是重现问题的代码: --- title: "Dashboard" output: flexdashboard::flex_dashboard:
flexdashboard
和Shining
创建一个小仪表板。它包含许多会计列,在这些列中,能够对值进行排序至关重要。作为表的一部分,在呈现表之前,我使用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)
)
```
---
标题:“仪表板”
输出:
flexdashboard::flex_仪表板:
方向:行
垂直布局:滚动
运行时间:闪亮
---
```{r包,include=F}
图书馆(DT)
库(格式化表)
图书馆(dplyr)
```
```{r}
签名\u格式化程序0,“绿色”,
ifelse(x<0,“红色”、“黑色”))
```
结果{数据高度=800数据宽度=100%}
---------------------------------------------
```{r data}
df%
as.datatable(,class='cell border stripe table striped',
筛选器='top',转义=FALSE,选择='none',
选项=列表(自动宽度=真,
server=TRUE,
fixedHeader=TRUE,
columnDefs=list(list(宽度为'200px',目标为c(1)))
),rownames=FALSE)
)
```
下面的小图片说明了我排序时出现的问题: