davidgohel / flextable

table farming
https://ardata-fr.github.io/flextable-book/
554 stars 79 forks source link

`bg()` function is not referencing the right column by index #557

Closed Aurelhoo closed 1 year ago

Aurelhoo commented 1 year ago

colourer <- function(x) ifelse(x == 3.9, "blue", "red")

Works fine when no column is hidden

flextable(head(mtcars)) |> bg( j = 5, bg = colourer)


<img src="https://i.imgur.com/IOi0k2B.png" width="1650" />

``` r

# bg in the right column but the function is based on the values of the previous column
flextable(head(mtcars), col_keys = names(mtcars)[-1]) |>
  bg(j = 4, bg = colourer) |> # not coloring drat == 3.9
  bg(j = 5, bg = colourer) # coloring 'wt' where drat == 3.9

# when using j with colum name it works fine
flextable(head(mtcars), col_keys = names(mtcars)[-1]) |>
  bg(j = "drat", bg = colourer) 

Created on 2023-07-26 with reprex v2.0.2

R version 4.3.1 (2023-06-16 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale:
[1] LC_COLLATE=Portuguese_Brazil.utf8  LC_CTYPE=Portuguese_Brazil.utf8    LC_MONETARY=Portuguese_Brazil.utf8 LC_NUMERIC=C                      
[5] LC_TIME=Portuguese_Brazil.utf8    

time zone: America/Sao_Paulo
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] flextable_0.9.3.002 shiny_1.7.4.1      

loaded via a namespace (and not attached):
 [1] sass_0.4.7              utf8_1.2.3              fontLiberation_0.1.0    xml2_1.3.5              httpcode_0.3.0         
 [6] digest_0.6.33           magrittr_2.0.3          evaluate_0.21           grid_4.3.1              fastmap_1.1.1          
[11] jsonlite_1.8.7          zip_2.3.0               processx_3.8.2          crul_1.4.0              ps_1.7.5               
[16] promises_1.2.0.1        fansi_1.0.4             fontBitstreamVera_0.1.1 textshaping_0.3.6       jquerylib_0.1.4        
[21] cli_3.6.1               rlang_1.1.1             fontquiver_0.2.1        crayon_1.5.2            ellipsis_0.3.2         
[26] reprex_2.0.2            withr_2.5.0             cachem_1.0.8            gfonts_0.2.0            yaml_2.3.7             
[31] gdtools_0.3.3           officer_0.6.2           tools_4.3.1             uuid_1.1-0              memoise_2.0.1          
[36] httpuv_1.6.11           curl_5.0.1              vctrs_0.6.3             R6_2.5.1                mime_0.12              
[41] lifecycle_1.0.3         fs_1.6.3                miniUI_0.1.1.1          ragg_1.2.5              pkgconfig_2.0.3        
[46] callr_3.7.3             clipr_0.8.0             pillar_1.9.0            bslib_0.5.0             later_1.3.1            
[51] glue_1.6.2              data.table_1.14.8       Rcpp_1.0.11             systemfonts_1.0.4       xfun_0.39              
[56] tibble_3.2.1            rstudioapi_0.15.0       knitr_1.43              xtable_1.8-4            htmltools_0.5.5        
[61] rmarkdown_2.23          compiler_4.3.1          askpass_1.1             openssl_2.1.0          
davidgohel commented 1 year ago

The issue is closed because it's fixed and the documentation mention clearly what you are describing.

Capture d’écran 2023-07-26 à 17 04 22

Did I miss something?

Aurelhoo commented 1 year ago

Sorry I missed that. But still, the default value for source should be different to not cause this unintuitive behavior when bg is a function.

j = 4 is referring to "drat" while source = 4 is referring to "hp" and the default is source = j = 4

davidgohel commented 1 year ago

j only accept values from colkeys

you did not used colkeys values but an integer.

source = j = 4 That is wrong, you drop the first name, so you display p columns but the internal dataset contain p+1 columns.