gdemin / expss

expss: Tables and Labels in R
https://cran.r-project.org/web/packages/expss/
84 stars 16 forks source link

Question about #Total cases ... #87

Closed robertogilsaura closed 3 years ago

robertogilsaura commented 3 years ago

Hi @gdemin

I hope you had an excellent summer.

I would like to ask if it is possible to automate that the row "#Total cases" always comes out in bold-type, when I use as.datatable_widget (), no matter if it shows above or below.

my code ...

mtcars %>% 
     tab_cells(cyl) %>% 
     tab_cols(total(), am) %>% 
     tab_stat_cpct() %>% 
     tab_pivot() %>%
     as.datatable_widget()

thanks in advance

Regards

robertogilsaura commented 3 years ago

Hi, @gdemin

I found this solution ...

mtcars %>% 
     tab_cells(cyl) %>% 
     tab_cols(total(), am) %>% 
     tab_stat_cpct() %>% 
     tab_pivot() %>%
     as.datatable_widget() >%> 
     formatStyle(.,columns = 1:999 ,target = 'row',fontWeight = styleEqual(c('#Total cases', 0),c(900,300)))

If somebody get better solution ...

Regards and thanks you.

gdemin commented 3 years ago

Hi, @robertogilsaura You found rather good solution. You can save a function and use it everywhere:

as_datatable_bold = . %>%
     as.datatable_widget() %>% 
     formatStyle(.,columns = 1:999 ,target = 'row',fontWeight = styleEqual(c('#Total cases', 0),c(900,300)))

Another solution is explicitly specify total label as bold:

mtcars %>% 
    tab_total_label("#<b>Total</b>") %>% 
    tab_cells(cyl) %>% 
    tab_cols(total(), am) %>% 
    tab_stat_cpct() %>% 
    tab_pivot() %>%
    as.datatable_widget(escape = FALSE)

Drawback here is escape = FALSE which means that all text which looks like HTML will be interpreted by datatable as HTML. Sometimes it is highly undesirable.

robertogilsaura commented 3 years ago

Hi @gdemin

Thanks for your answer. Client needs not only the label, but also the values (whole row). In the end, it was not only bold but with background and changing the font :( !!!

mtcars %>% tab_cols(total(), am) %>% tab_cells(vs) %>% tab_stat_cases() %>% tab_pivot %>% as.datatable_widget() %>%
     formatStyle(
          .,
          columns = 0:9999,
          target = 'row',
          fontWeight = styleEqual('#Total cases', 900),
          color =  styleEqual('#Total cases', 'white'),
          backgroundColor = styleEqual('#Total cases', 'cadetblue')
     )

Thanks again. expss is a great package!!! Many customers like Kantar, IQ Nielsen and other smaller companies are discovering your package with us ...