Search code examples
rhtml-tableformattingcss-tablesexpss

Adjusting Column Widths with expss Package


I've been using expss frequently and find it very useful, however, in some cases I have column values that are long strings which do not fit into the default column widths.

For example, the image below shows a column that needs widening. What htmlTables() options in expss can be used to widen columns? enter image description here


Solution

  • There are several methods to increase width of the first column. Try one of the following:

    library(expss)
    expss_output_viewer()
    
    data(mtcars)
    mtcars = apply_labels(mtcars,
                          mpg = "Miles/(US) gallon",
                          cyl = "Number of cylinders",
                          disp = "Displacement (cu.in.)",
                          hp = "Gross horsepower",
                          drat = "Rear axle ratio",
                          wt = "Weight (1000 lbs)",
                          qsec = "1/4 mile time",
                          vs = "Engine",
                          vs = c("V-engine" = 0,
                                 "Straight engine" = 1),
                          am = "Transmission",
                          am = c("Automatic" = 0,
                                 "Manual"=1),
                          gear = "Number of forward gears",
                          carb = "Number of carburetors"
    )
    
    
    # straightforward column width adjustment
    calc_cro_cpct(mtcars, list(vs, am), list(total(), vs %nest% am)) %>%
        htmlTable(., css.cell = c("width: 250px", # first column width
                                  rep("width: 50px", ncol(.) - 1)) # other columns width
        )
    
    # row labels padding
    calc_cro_cpct(mtcars, list(vs, am), list(total(), vs %nest% am)) %>%
        htmlTable(padding.rgroup = paste(rep(" ", 20), collapse = ""))
    
    # disable row grouping
    calc_cro_cpct(mtcars, list(vs, am), list(total(), vs %nest% am)) %>%
        htmlTable(row_groups = FALSE)
    

    Arguments css.cell and padding.rgroup are documented in the htmlTable package which expss use for rendering HTML table representation (see ?htmlTable::htmlTable).