简体   繁体   English

DT数据表显示错误

[英]DT data table display error

I have a data.table I am displaying with DT.我有一个用 DT 显示的 data.table。 In two columns I display percentages and want to show background bars.在两列中,我显示百分比并希望显示背景条。 However the number of columns can change depending on the table chosen.但是,列数可以根据所选的表而变化。 There will either be one or two percentage columns.将有一个或两个百分比列。

Here's some dummy data and my approach so far which unsuccessfully uses grep.这是一些虚拟数据和我迄今为止使用 grep 失败的方法。

a <- c(45, 143, 123, 120, 118, 109, 94, 81)
b <- c(54, 132, 119, 113, 108, 104, 99, 91)
a2 <- round(a/sum(a)*100,2)
b2 <- round(b/sum(b)*100,2)

dt <- data.table("Age" = c("-20", "20-30", "30-40", "40-50",
                           "50-60", "60-70", "70-80", "80+"),
                 "Group A" = a,
                 "Group A %" = a2,
                 "Group B" = b,
                 "Group B %" = b2)


if(sample(c(0,1), 1)==1) x <- dt else x <- dt[ ,c(1:3)]

DT::datatable(x,
              rownames = FALSE,
              extensions = c('FixedColumns'),
              class = 'cell-border stripe',
              options = list(dom = 't',
                             pageLength = nrow(x),
                             columnDefs = list(list(className = 'dt-center', targets = 0:(ncol(x)-1)))
                             )
              ) %>%
  formatStyle(
    grep("%", colnames(x), value=TRUE),
    background = styleColorBar(x[, .SD, .SDcols=grep("%", colnames(x), value=TRUE)], 'steelblue'),
    backgroundSize = '50% 50%',
    backgroundRepeat = 'no-repeat',
    backgroundPosition = 'right')

Unfortunately this create the error:不幸的是,这会产生错误:

*Error in FUN(X[[i]], ...) : only defined on a data frame with all numeric variables*

So how can I dynamically select the columns to display the bars?那么如何动态选择列来显示条形呢?

Any help much appreciated.非常感谢任何帮助。

The problem comes from range() in styleColorBar() , as it expects a numeric or character vector.问题来自styleColorBar() range()中的styleColorBar() ,因为它需要一个数字或字符向量。

If you pass only the values of the columns you want to apply css to it will work.如果您只传递要对其应用 css 的列的值,它将起作用。 You can use styleColorBar = unlist(x[, .SD, .SDcols=grep("%", colnames(x), value=TRUE)]) to do just that:您可以使用styleColorBar = unlist(x[, .SD, .SDcols=grep("%", colnames(x), value=TRUE)])来做到这一点:

DT::datatable(x,
              rownames = FALSE,
              extensions = c('FixedColumns'),
              class = 'cell-border stripe',
              options = list(dom = 't',
                             pageLength = nrow(x),
                             columnDefs = list(list(className = 'dt-center', targets = 0:(ncol(x)-1)))
              )
) %>%
  formatStyle(
    columns = grep("%", colnames(x), value=TRUE),
    background = styleColorBar(unlist(x[, .SD, .SDcols=grep("%", colnames(x), value=TRUE)]), 'steelblue'),
    backgroundSize = '50% 50%',
    backgroundRepeat = 'no-repeat',
    backgroundPosition = 'right')

This will return这将返回

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM