使用formatstyle数据表r

时间:2018-08-14 03:57:42

标签: r dt

我有一个反应性数据框,如下所示:

       Apr 2017   May 2017   Jun 2017   Jul 2017   Aug 2017   Sep 2017
zz    0.1937571  0.1840005  0.1807256  0.1959589  0.2039463  0.2016886
aa    0.3518203  0.3634578  0.3670747  0.3676495  0.3680581  0.3657724
bb   0.10651308 0.11548379 0.11572389 0.11272168 0.11361587 0.11503638
cc    0.2481513  0.2579199  0.2623222  0.2673914  0.2579430  0.2550686
dd   0.06641069 0.06741159 0.07305105 0.07373854 0.07043972 0.07304338

我正在尝试根据值(类似于eg3)对整个表进行样式设置。 下面是我的代码:

brks <- reactive({
    quantile(intrc_pattern_re(), probs = seq(0, 1, 0.25), na.rm = TRUE)
})

clrs <- reactive({
    round(seq(255, 40, length.out = length(brks()) + 1), 0) %>%
        paste0("rgb(255,", ., ",", ., ")")
})

intrc_pattern_reshape <- reactive ({
    datatable(intrc_pattern_re(),
              options = list(searching = FALSE,
                             pageLength = 15,
                             lengthChange = FALSE)
             ) %>%
        formatPercentage(colnames(intrc_pattern_re()), 2) %>%
        formatStyle(names(intrc_pattern_re()),
                    backgroundColor = styleInterval(brks(), clrs()))
})

但是当我这样做时,出现以下错误:non-numeric argument to binary operator

有人可以告诉我我做错了什么吗?谢谢。 dput(df,“”)

的输出
structure(list(`Apr 2017` = structure(c(`zz` = 3L, 
aa = 6L, `bb` = 2L, `cc` = 4L, 
dd = 1L, Premium = 7L, `ff` = 5L), .Label = c("0.06641069", 
"0.10651308", "0.1937571", "0.2481513", "0.3090870", "0.3518203", 
"0.4697810", "Apr 2017"), class = "factor"), `May 2017` = structure(c(`zz` = 3L, 
aa = 6L, `bb` = 2L, `cc` = 4L, 
dd = 1L, Premium = 7L, `ff` = 5L), .Label = c("0.06741159", 
"0.11548379", "0.1840005", "0.2579199", "0.3043959", "0.3634578", 
"0.4719425", "May 2017"), class = "factor"), `Jun 2017` = structure(c(`zz` = 3L, 
aa = 6L, `bb` = 2L, `cc` = 4L, 
dd = 1L, Premium = 7L, `ff` = 5L), .Label = c("0.07305105", 
"0.11572389", "0.1807256", "0.2623222", "0.3030102", "0.3670747", 
"0.4766237", "Jun 2017"), class = "factor"), `Jul 2017` = structure(c(`zz` = 3L, 
aa = 6L, `bb` = 2L, `cc` = 4L, 
dd = 1L, Premium = 7L, `ff` = 5L), .Label = c("0.07373854", 
"0.11272168", "0.1959589", "0.2673914", "0.2984132", "0.3676495", 
"0.4759238", "Jul 2017"), class = "factor"), `Aug 2017` = structure(c(`zz` = 3L, 
aa = 6L, `bb` = 2L, `cc` = 4L, 
dd = 1L, Premium = 7L, `ff` = 5L), .Label = c("0.07043972", 
"0.11361587", "0.2039463", "0.2579430", "0.2970350", "0.3680581", 
"0.4828409", "Aug 2017"), class = "factor"), `Sep 2017` = structure(c(`zz` = 3L, 
aa = 6L, `bb` = 2L, `cc` = 4L, 
dd = 1L, Premium = 7L, `ff` = 5L), .Label = c("0.07304338", 
"0.11503638", "0.2016886", "0.2550686", "0.2998945", "0.3657724", 
"0.4909182", "Sep 2017"), class = "factor"), `Oct 2017` = structure(c(`zz` = 3L, 
aa = 6L, `bb` = 2L, `cc` = 4L, 
dd = 1L, Premium = 7L, `ff` = 5L), .Label = c("0.07651393", 
"0.11219458", "0.2025043", "0.2479362", "0.2866641", "0.3673334", 
"0.5121613", "Oct 2017"), class = "factor"), `Nov 2017` = structure(c(`zz` = 3L, 
aa = 6L, `bb` = 1L, `cc` = 4L, 
dd = 2L, Premium = 7L, `ff` = 5L), .Label = c("0.10724728", 
"0.15016708", "0.1857769", "0.2280702", "0.2691103", "0.3417920", 
"0.4948308", "Nov 2017"), class = "factor"), `Dec 2017` = structure(c(`zz` = 2L, 
aa = 5L, `bb` = 1L, `cc` = 3L, 
dd = 6L, Premium = 7L, `ff` = 4L), .Label = c("0.08775835", 
"0.1659323", "0.1945492", "0.2304338", "0.2958437", "0.29888712", 
"0.4493300", "Dec 2017"), class = "factor"), `Jan 2018` = structure(c(`zz` = 2L, 
aa = 5L, `bb` = 1L, `cc` = 3L, 
dd = 6L, Premium = 7L, `ff` = 4L), .Label = c("0.08016616", 
"0.1565603", "0.1753247", "0.2134740", "0.2811306", "0.34148205", 
"0.4315794", "Jan 2018"), class = "factor")), row.names = c("zz", 
"aa", "bb", "cc", "dd", 
"Premium", "ff"), class = "data.frame")

1 个答案:

答案 0 :(得分:5)

当您将非non-numeric argument to binary operator类型的内容传递给诸如numeric+之类的二进制运算符时,将发生-错误。例如:

> 'a'+3
Error in "a" + 3 : non-numeric argument to binary operator

调用quantile时收到此错误,因为您以intrc_pattern_re()传递的data.frame中的所有数字都被错误地分类为factors而不是{{ 1}}。如果查看numeric的输出,则会看到每一行都显示dput

class = "factor"))中的某个地方是一个二进制运算符,期望接收到quantile并在得到numeric时引发错误。

要解决此问题,您只需将factor返回的data.frame的每一列改成intrc_pattern_re()

如果我们将您的数据框加载为numeric

df

如果我们将这些quantile(x, probs = seq(0, 1, 0.25), na.rm = TRUE) Error in (1 - h) * qs[i] : non-numeric argument to binary operator 变量转换为factor(请注意,您必须先转换为numeric,然后转换为character),那么它将起作用:

numeric