使用formatstyle datatable r时出错

SNT*_*SNT 7 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
Run Code Online (Sandbox Code Playgroud)

我想样式基于价值全表(与此类似,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()))
})
Run Code Online (Sandbox Code Playgroud)

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

有人能告诉我,我做错了什么吗?谢谢.输出的输出(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")
Run Code Online (Sandbox Code Playgroud)

div*_*san 5

你得到的错误:non-numeric argument to binary operator当你传递的东西,这不是类型的情况numeric,以二进制运算符像+-.例如:

> 'a'+3
Error in "a" + 3 : non-numeric argument to binary operator
Run Code Online (Sandbox Code Playgroud)

您从调用中收到此错误,quantile因为您传入的data.frame中的所有数字intrc_pattern_re()都被错误地归类为factors不正确numeric.如果你查看输出dput,你会看到每一行都说明了class = "factor"))

某处quantile是一个二元运算符,它期望在获得a 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
Run Code Online (Sandbox Code Playgroud)

如果我们将这些factor变量转换为numeric(注意你必须首先转换为character然后numeric),那么它可以工作:

df2 <- df %>%
    dplyr::mutate_if(is.factor, function(x) as.numeric(as.character(x)))

quantile(df2, probs = seq(0, 1, 0.25), na.rm = TRUE)
        0%        25%        50%        75%       100% 
0.06641069 0.15176539 0.25160995 0.34171451 0.51216130
Run Code Online (Sandbox Code Playgroud)