kableExtra icon indicating copy to clipboard operation
kableExtra copied to clipboard

cell_spec(): align and italic option not working

Open stvrd opened this issue 5 years ago • 4 comments

It seems to me that the align and italic options of cell_spec do not function properly:

mtcars[1:5, 1:4] %>%
  mutate(
    car = row.names(.),
    hp  = cell_spec(hp,background = "red", color = "white", italic=T, align = "center")
  ) %>%
  select(car, mpg, cyl, hp) %>%
  kable(escape = F) %>%
  kable_styling("hover", full_width = F)

image

> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=French_Switzerland.1252  LC_CTYPE=French_Switzerland.1252    LC_MONETARY=French_Switzerland.1252
[4] LC_NUMERIC=C                        LC_TIME=French_Switzerland.1252    

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

other attached packages:
[1] bindrcpp_0.2.2   magrittr_1.5     dplyr_0.7.7      kableExtra_0.9.0

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.19      rstudioapi_0.8    bindr_0.1.1       knitr_1.20        xml2_1.2.0       
 [6] hms_0.4.2         tidyselect_0.2.4  rvest_0.3.2       munsell_0.5.0     viridisLite_0.3.0
[11] colorspace_1.3-2  R6_2.3.0          rlang_0.2.2       stringr_1.3.1     httr_1.3.1       
[16] tools_3.5.1       webshot_0.5.1     htmltools_0.3.6   assertthat_0.2.0  yaml_2.2.0       
[21] digest_0.6.17     rprojroot_1.3-2   tibble_1.4.2      crayon_1.3.4      purrr_0.2.5      
[26] readr_1.1.1       glue_1.3.0        evaluate_0.12     rmarkdown_1.10    stringi_1.1.7    
[31] compiler_3.5.1    pillar_1.3.0      scales_1.0.0      backports_1.1.2   pkgconfig_2.0.2  

stvrd avatar Nov 09 '18 12:11 stvrd

Italic option doesn't work in Rstudio viewer for me too but works well in knit document (with chrome). I think the align argument doesn't work because it is apply to the <span> element and not the <td> element which have a css property text-align: specify from the kable() function. So you can use the align argument in kable() function to specify alignement for each column

mtcars[1:5, 1:4] %>%
  mutate(
    car = row.names(.),
    hp  = cell_spec(hp,background = "red", color = "white", italic=T)
  ) %>%
  select(car, mpg, cyl, hp) %>%
  kable(escape = F, align = c("lrrc")) %>%
  kable_styling("hover", full_width = F)

capture2

jrmie avatar Dec 08 '18 15:12 jrmie

It appears this is not fixed, yet. Alignment of specific cells with cell_spec is still failing! See also the thread on stack overflow. Is this about to be fixed?

mrcaseb avatar Apr 20 '20 09:04 mrcaseb

This seems to still be an issue, but I believe that it's an issue with the CSS, rather than the simply the text-align parameter in the <td> and <span> tags. Adding float: right; to cells that you wish to be right aligned seems to fix this problem (this also overwrites whatever is automatically put in the <td> tag. So the below would work to right-align a cell:

x <- cell_spec(x,align="r",extra_css="float:right;")

The below function can allow a vectorised alignment and can take any of the other parameters usually passed to cell_spec() as they will just get forwarded on to it anyway. However, this is obviously a work-around and so it would be useful to have some sort of check in the cell_spec() function that will add float:right; where needed.

cell_realign <- function(x,left_align=T,...){
  if(length(left_align)==1){
    if(left_align)
    {
      cell_spec(x,align="left",...)
    } else {
      cell_spec(x,align="right",extra_css="float:right;",...)
    }
  } else if(length(x) != length(left_align)){
    stop("length of x & left_align do not match in cell_realign()",call.=F)
  } else {
    vapply(1:length(x),
           FUN = function(i) cell_realign(x[[i]],left_align[[i]],...),
           FUN.VALUE = character(1))
  }
}

MyKo101 avatar Oct 14 '20 10:10 MyKo101

This seems to still be an issue, but I believe that it's an issue with the CSS, rather than the simply the text-align parameter in the <td> and <span> tags. Adding float: right; to cells that you wish to be right aligned seems to fix this problem (this also overwrites whatever is automatically put in the <td> tag. So the below would work to right-align a cell:

x <- cell_spec(x,align="r",extra_css="float:right;")

The below function can allow a vectorised alignment and can take any of the other parameters usually passed to cell_spec() as they will just get forwarded on to it anyway. However, this is obviously a work-around and so it would be useful to have some sort of check in the cell_spec() function that will add float:right; where needed.

cell_realign <- function(x,left_align=T,...){
  if(length(left_align)==1){
    if(left_align)
    {
      cell_spec(x,align="left",...)
    } else {
      cell_spec(x,align="right",extra_css="float:right;",...)
    }
  } else if(length(x) != length(left_align)){
    stop("length of x & left_align do not match in cell_realign()",call.=F)
  } else {
    vapply(1:length(x),
           FUN = function(i) cell_realign(x[[i]],left_align[[i]],...),
           FUN.VALUE = character(1))
  }
}

it's worked for me, thank you!

driapitek avatar Mar 19 '21 13:03 driapitek