expss

How do you remove the row_labels text in an expss table?


I like to pipe my expss tables into kable to get access to some additional formatting options. That sometimes requires some tweaking, and I'm looking for a tweak here to get rid of the row_labels text in the first column of the header in the example below.

Simple reprex:

df <- data.frame(x=rbinom(100,1,0.5), y=rnorm(100,1,0.6),
                 z=rnorm(100,1,0.2), grp = rep(1:5,20))
var_lab(df$grp) = ""
df %>%
  tab_cells(x,y,z) %>%
  tab_cols(grp) %>%
  tab_stat_mean (label = "") %>%
  tab_pivot %>%
  kable(caption= "Title",
        digits = c(0,rep(3,5))) %>%
  kable_styling(full_width=F, position="center", 
                bootstrap_options = c("striped"))%>%
  add_header_above(c("", "Group" = 5))

Generates this:

html table

Thanks!


Solution

  • It's better to use 'htmlTable' or 'huxtable' for output expss tables. It is because they are both support complex multilevel and multinested headers. However, if you want to use 'kable' you can set first column name to empty string just after 'tab_pivot':

    library(expss)
    library(knitr)
    library(kableExtra)
    
    # function which remove first column name
    remove_first_name = function(x){
        setNames(x, c("", names(x)[-1]))
    }
    
    
    df <- data.frame(x=rbinom(100,1,0.5), y=rnorm(100,1,0.6),
                     z=rnorm(100,1,0.2), grp = rep(1:5,20))
    var_lab(df$grp) = ""
    df %>%
        tab_cells(x,y,z) %>%
        tab_cols(grp) %>%
        tab_stat_mean (label = "") %>%
        tab_pivot %>%
        remove_first_name %>%  # remove 'row_labels'
        kable(caption= "Title",
              digits = c(0,rep(3,5))) %>%
        kable_styling(full_width=F, position="center", 
                      bootstrap_options = c("striped"))%>%
        add_header_above(c("", "Group" = 5))