【问题标题】:Error while using formatstyle datatable r使用格式样式数据表 r 时出错
【发布时间】:2019-01-20 20:14:08
【问题描述】:

我有一个响应式数据框如下:

       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")

【问题讨论】:

  • 您的代码中如何引用这些数据? intrc_pattern_re() 是否返回您显示的数据框?
  • @divibisan intrc_pattern() 是一个错误。 intrc_pattern_re() 返回我上面提到的结构的df。
  • 您撤回接受的答案了吗?你还有这个问题吗?
  • @divibisan 我实际上认为我发送的是 df 而不是 df 的专栏以获得休息时间。我从正确的 df 传递了正确的列,它对我来说效果很好。谢谢你的回答。

标签: r dt


【解决方案1】:

当您将不属于numeric 类型的内容传递给+- 之类的二元运算符时,会出现以下错误:non-numeric argument to binary operator。例如:

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

您在调用quantile 时收到此错误,因为您作为intrc_pattern_re() 传入的data.frame 中的所有数字都被错误地分类为factors 而不是numeric。如果你查看dput 的输出,你会看到每一行都写着class = "factor"))

quantile 中的某处是一个二元运算符,它期望接收 numeric 并在收到 factor 时抛出错误。

要解决这个问题,你只需要将intrc_pattern_re()返回的data.frame的每一列都变成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),那么它就可以工作:

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

【讨论】:

    猜你喜欢
    • 2015-09-12
    • 1970-01-01
    • 2021-12-13
    • 2016-11-15
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    • 2021-09-22
    • 1970-01-01
    相关资源
    最近更新 更多