flextable
flextable copied to clipboard
Allow scales::col_numeric()'s `domain` to exclude some values in the table - currently throws an error
Hi! First off, thanks for this great package, I'm always amazed at all it can achieve.
I've come across what I think is a bug, but it may be a deliberate feature since the update that you mention in #395. I had created a table containing p-values, where the background of the cells was coloured using scales::col_numeric() as suggested, but only if they fell within a certain range (p < 0.05).
colourer <- scales::col_numeric(
palette = c("darkgreen", "green"),
domain = c(0, 0.05), na.color = "#FFFFFF"
)
my_data <- tibble(p1 = c(0.5, 0.01, 0.1, 0.002),
p2 = c(0.5, 0.02, 0.1, 0.001))
my_data %>%
flextable::flextable() %>%
flextable::bg(part = "body", bg = colourer)
Since updating the package, I get the following error:
Error: a warning occured while using color function: Some values were outside the color scale and will be treated as NA
I realise this may be deliberate, but restricting the domain to those values (p between 0 and 0.05) makes good use of the colour palette within the range we're interested in, while allowing us to ignore other values. Would it be possible to throw a warning that still allows the table to be created?
Thanks!
I've found a work-around of turning the values we're not interested in into NAs before piping into flextable in the meantime. Happy to stick with this if that's the best plan.
my_data <- tibble(comparison = c("A", "B", "C", "D"),
p1 = c(0.5, 0.01, 0.1, 0.002),
p2 = c(0.5, 0.02, 0.1, 0.001))
my_data %>%
mutate_at(-1, function(x) ifelse(x < 0.05, x, NA)) %>%
flextable::flextable() %>%
flextable::bg(j = 2:3, part = "body", bg = colourer)
Hello,
Could you post your sessionInfo() please as asked in the guidelines? I am confused because I can't reproduce your issue.
I have a warning (from scales::col_numeric()) but no error.
library(flextable)
library(magrittr)
library(tibble)
colourer <- scales::col_numeric(
palette = c("darkgreen", "green"),
domain = c(0, 0.05), na.color = "red"
)
my_data <- tibble(p1 = c(0.5, 0.01, 0.1, 0.002),
p2 = c(0.5, 0.02, 0.1, 0.001))
my_data %>%
flextable() %>%
bg(part = "body", bg = colourer)
#> Warning: Some values were outside the color scale and will be treated as NA
#> Warning: Some values were outside the color scale and will be treated as NA
Created on 2022-08-07 by the reprex package (v2.0.1)
Sorry for the delay in getting back to you, and for the omission.
Here's the session info. I just reproduced the error using the code snippet you provided in a new R session.
R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)
Matrix products: default
locale:
[1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C
[5] LC_TIME=English_United Kingdom.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] tibble_3.1.7 magrittr_2.0.3 flextable_0.7.2
loaded via a namespace (and not attached):
[1] Rcpp_1.0.7 rstudioapi_0.13 knitr_1.39 xml2_1.3.3
[5] munsell_0.5.0 uuid_1.0-3 colorspace_2.0-3 R6_2.5.1
[9] rlang_1.0.2 fastmap_1.1.0 fansi_1.0.3 tools_4.1.2
[13] data.table_1.14.2 xfun_0.31 utf8_1.2.2 cli_3.3.0
[17] htmltools_0.5.2 systemfonts_1.0.3 ellipsis_0.3.2 digest_0.6.29
[21] lifecycle_1.0.1 crayon_1.5.1 zip_2.2.0 farver_2.1.0
[25] officer_0.4.1 vctrs_0.4.1 base64enc_0.1-3 glue_1.6.2
[29] evaluate_0.15 rmarkdown_2.14 compiler_4.1.2 pillar_1.7.0
[33] scales_1.2.0 gdtools_0.2.3 pkgconfig_2.0.3
The error message was:
Error: a warning occured while using color function: Some values were outside the color scale and will be treated as NA
No problem for the delay:)
Thanks, this has been fixed in version 0.7.3. Sorry for the inconvenience. Now, you would get a warning as in my previous reprex instead of an error. This warning just display the warning coming from package 'scales'.
I do not recommend using your workaround because it changes the data (although you get your work done!). I would instead either accept the warning from 'scales', either write a wrapper around it as shown below; that way, we don't need to change the data.
library(dplyr)
colourer <- function(x, myrange = c(0, 0.05)){
scale_colourer <- scales::col_numeric(
palette = c("darkgreen", "green"),
domain = myrange, na.color = "#FFFFFF"
)
colors <- rep("transparent", length(x))
withinrange <-between(x, left = myrange[1], right = myrange[2])
colors[withinrange] <- scale_colourer(x[withinrange])
colors
}
my_data <- tibble(p1 = c(0.5, 0.01, 0.1, 0.002),
p2 = c(0.5, 0.02, 0.1, 0.001))
my_data %>%
flextable::flextable() %>%
flextable::bg(part = "body", bg = colourer)
This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.