I have a reactive dataframe as following:
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
I am trying to style the full table based on values(similar to this,eg3). Below is the code I have :
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()))
})
But when I do that I get the following error : non-numeric argument to binary operator
Could someone tell me what is that I am doing incorrectly? Thank you. The output for 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")
The error you're getting: non-numeric argument to binary operator
happens when you pass something that's not of type numeric
to a binary operator like +
or -
. For example:
> 'a'+3
Error in "a" + 3 : non-numeric argument to binary operator
You're getting this error from your call to quantile
because all the numbers in the data.frame you're passing in as intrc_pattern_re()
are incorrectly classified as factors
not numeric
. If you look at the output of dput
, you see that each line says class = "factor"))
Somewhere in quantile
is a binary operator that is expecting to receive a numeric
and throws the error when it gets a factor
.
To solve this, you just need to make each column of the data.frame returned by intrc_pattern_re()
into numeric
.
If we load your data frame as df
:
quantile(x, probs = seq(0, 1, 0.25), na.rm = TRUE)
Error in (1 - h) * qs[i] : non-numeric argument to binary operator
If we convert these factor
variables to numeric
(note you must first convert to character
and then numeric
) then it works:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With