setWidths: Set column widths of a table

View source: R/rmd.util.r

setWidthsR Documentation

Set column widths of a table

Description

Properly setting column widths given the maximum width of the table, for non-html output only.

Usage

setWidths(x, header1, maxTableWidth = 7, rowHeaderInd = NULL,
          minFontSize=9, nRowPerRowHeader=NULL)
breakRatio(aStr)

Arguments

x

A flextable object

header1

(character vector) the first row of the column header

maxTableWidth

(numeric, 7.0) the maximum width of the table in inch. The default 7.0 corresponds to "PAGE LAYOUT" > "size" > "letter"; "Margins" > "Moderate" in MS Word.

rowHeaderInd

(integer, NULL) See rowHeaderInd in rmdTable. Because row headers are bold font, their lengths in inch is increased by 10%.

minFontSize

(integer, 9) The minimum font size in table body.

nRowPerRowHeader

(integer vector, NULL) The average number of rows spanned by each row header under a row header index. So it's a vector of length rowHeaderInd.

aStr

(a character string) The string is the content of a header cell.

Details

setWidths sets the width of each column of a table to fit maxTableWidth. Here is a brief description of its algorithm:

let HBWidths = mapply(max, wHeader,wBody)

  1. if(sum(HBWidths) <= maxTableWidth), set HBWidths to be the final table widths.

  2. Else if(sum(HBWidths)/maxTableWidth < 1.08), reducing font size by 1, set HBWidths*0.92 to be the final table widths.

  3. Else

    1. For columns where the header is longer than the body and the header is a single word, wrap the header at a non-letter character closest to the middle of the header.

    2. If the table still doesn't fit, further wrapping the columns of row headers if any, and if the cells under those columns span vertically across multiple cells.

    3. If the table still doesn't fit, reduce font size up to minFontSize

    4. If the table still doesn't fit, wrap the table body, staring from the longest table columns, until the table fits.

breakRatio computes the wrapping point of a header.

Value

(numeric) by breakRatio, the ratio of the original length after wrapping. (list) by setWidths. The list contains 1)widths: a numeric vector of column widths and 2)fs: the new font size.


blueskypie/wfr documentation built on Feb. 6, 2024, 4:38 p.m.