co-analysis / a11ytables

R package: generate best-practice stats spreadsheets for publication
https://co-analysis.github.io/a11ytables/
Other
42 stars 3 forks source link

Make wrapping default and column-width-sizing smarter #41

Closed matt-dray closed 2 years ago

matt-dray commented 2 years ago

Cells with a large number of characters either wrap awkwardly or aren't wrapped. We need a sensible solution to decide things like cell widths.

Wrapping should probably be default in cells anyway. Probably not in pre-table elements (title, table count, note presence, data source), otherwise the title could get wrapped (usually not ideal) or the first column could become very wide, which would look odd if the contents have few characters.

In an example notes table that contains two columns (e.g. a lookup of question codes in column A, to full question labels in column B), the labels might get into the tens to hundreds of characters long. Default wrapping would help for the shorter ones, but it would help to detect maximum character length and adjust the column width as necessary. Don't make the column wide enough to fit the whole text on one line, but choose a sensible maximum width where the wrapping can then take over if there's overspill.

Ultimately, the success of these approaches is dependent on case-by-case requirements and it will be left to the user to tweak the output as they see fit.

Current way of setting column widths in 'tables' sheet types:

  openxlsx::setColWidths(
    wb = wb,
    sheet = tab_title,
    cols = seq(table_width),
    widths = 16  # <--- make this dynamic
  )
matt-dray commented 2 years ago

I defined 'smarter' in #49 as 'if max nchar is bigger than 50, then widen to twice the default'. This isn't 'smart' per se. May need to revisit this in future to actually make it 'smart' or at least choose better default widths (bearing in mind it will never be perfect for everyone).