rmdTable: Construct a table

View source: R/rmd.util.r

rmdTableR Documentation

Construct a table

Description

Construct a table in html, pdf, or word document, format the numeric columns, and automatically setting column widths.

Usage

rmdTable(dataDf, header = list(colnames(dataDf)), footer = NULL,
         colWidths = NULL, fontSize = 11, caption = NULL,
         rowHeaderInd = NULL, isDocx = TRUE, nRowScroll = 20,
         nRowDisplay = 200, maxTableWidth = 7,
         theme = c("zebra","box","booktabs","vanilla","tron","vader"),
         char2space=NULL, splitCamelCase=FALSE,
         footerFontSize=9, minFontSize=9,...)

myFlexTable(dataDf,header=list(colnames(dataDf)), footer = NULL,
            colWidths = NULL, fontSize = 11, caption = NULL,
            rowHeaderInd = NULL, mergeBodyColumn = TRUE, maxTableWidth = 7,
            theme = c("zebra","box","booktabs","vanilla","tron","vader"),
            char2space=NULL, splitCamelCase=FALSE,
            footerFontSize=9, minFontSize=9,...)

myKable(dataDf,header = list(colnames(dataDf)), footer = NULL,
        caption = NULL, rowHeaderInd = NULL, nRowScroll = 20,
        theme = c("zebra","box","vanilla"),...)

setFlexTableFontSize(ft,fontSize,footerFontSize=9)

Arguments

dataDf

(data.frame or matrix) the content of the table to be displayed

header

(character or list of character vectors, list(colnames(dataDf)))

  • list: each vector is the column title. The last vector replaces the colnames(dataDf). Neighboring cells of identical content in the header will be merged into one cell. For myKable, merge is only horizontal and not on the last row.

  • character: use '|' to separate cell, and '||' to separate rows. It is converted to list using str2list

footer

(character or list of character vectors, NULL)

  • list: each vector contains the following items:

    1. (optional, character). The cell content which the footnote refers to.

    2. (character). The content of the footer. use $~i$ and $^i$ to represent the sub/super-script of i

    3. (optional, character). The super-script of #1

    4. (optional, character, "header" or "body") the portion of the table where #1 is to be searched. If "body", only the columns in rowHeaderInd are searched.

  • character: use '|' to separate cell, and '||' to separate rows. It is converted to list using str2list

colWidths

(character or numeric vector, NULL). For myFlexTable only, the column widths, a numerical vector of the length of ncol(dataDf). Unit is inch. It can also be a character string where the numbers are separated by ',', e.g. "2,1,1,1".

fontSize

(integer, 10) For myFlexTable only, the font size of the header and body. Font size of footer is fontSize - 2.

caption

(character, NULL) the caption of the table.

rowHeaderInd

(integer, NULL). Row headers are the columns in the left of table body serving as headers for rows in the table body. rowHeaderInd are the last index of those columns, so the column indices of row headers is 1:rowHeaderInd. If rowHeaderInd is specified, the font of row header becomes bold, and neighboring cells of identical content are collapsed. The merge can be both horizontal and vertical in myFlexTable, and only vertical in myKable.

mergeBodyColumn

(boolean, TRUE) if(mergeBodyColumn && rowHeaderInd > 1), neighboring horizontal cells in table body are merged if they are in rows where exists identical neighboring horizontal cells in row header. This flag is to prevent, if set to FALSE, the merging of identical neighboring horizontal cells in table body, when the intention is to limit such merging to row header only.

isDocx

(boolean, TRUE) if TRUE, use myFlexTable; otherwise, myKable

nRowScroll

(integer, 20) For myKable only, the cutoff on number of rows to apply a scroll window.

nRowDisplay

(integer, 200) For myFlexTable only, the cutoff on number of rows to display. If there are more than nRowDisplay rows in the table, the caption of the table is appended "(top nRowDisplay rows only)".

theme

(character) The theme of the table.

maxTableWidth,minFontSize

see those parameters in setWidths. For myFlexTable only.

char2space

(character string, NULL) A regular expression. Should the characters represented by this regular expression in the bottom row of column header be changed to space? If so, when the column header is wrapped, the wrapping happens at a space in stead of the middle of a word. For example, setting char2space = '[^A-z0-9]' changes all non-letter and non-digit characters into space.

splitCamelCase

(boolean, FALSE) Should the camel cases in the 1st row of column header be split into separated words? for example, change "youMadeItLOL" into "you Made It LOL". If so, when a column header in camel case is wrapped, the wrapping happens at a space in stead of the middle of a word.

footerFontSize

(integer,9) For myFlexTable only; the size of footer font.

ft

a flextable object

intTypeCutoff

(integer, 10) if a number is an integer and its absolute value is less than intTypeCutoff, don't format. Set NULL to disable.

...

if isDocx, passed to flextable::flextable(), and kableExtra::kable() otherwise.

Details

myFlexTable and myKable are wrapper functions of flextable and kable, and rmdTable is a wrapper of the two wrappers with isDocx TRUE referring to myFlexTable and FALSE to myKable

Value

(flextable or kable object)

Note

Numeric columns are formatted using num2formattedStr. If a numeric column is not formatted in the displayed table, probably it is because its data type in dataDf is not numeric or integer.

See Also

num2formattedStr, flextable, kable

Examples

library(wfr)
df1=data.frame(A=c("a","a","b3"),
               B=c("b1","b2","b3"),
               C1=c(1001.123,58.04,32.01),
               C2=c(-0.00321, 0.0121, 0.325))

header=list(c('A','A','C','C'),
            c('A','A','C1','C2'))

footer=list(c("A","Arkansas$~ref$",'1','header'),
            c("C1","Kansas$^ref$",'x','header'),
            c('a',"Arizona",'2','body'))

rmdTable(df1,header = header,
           rowHeaderInd = 2,
           footer = footer,
           caption = "my first table",
           colWidths = c(2,1,1,1),
           fontSize = 12, isDocx = TRUE)

colWidths = "2,1,1,1"
header = "A | A | C | C || A | A | C1 | C2"
footer = "A|Arkansas$~ref$|1|header
               || C1|Kansas$^ref$|x|header
               || a|Arizona|2|body"

rmdTable(df1,header = header,
           rowHeaderInd = 2,
           footer = footer,
           caption = "my second table",
           colWidths = colWidths,
           fontSize = 12, isDocx = TRUE)

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