Customizing HTML tables"

knitr::opts_chunk$set(collapse = TRUE, comment = "#>", warning = FALSE, message = FALSE)

All tab_*-functions create a HTML page with the table output. This table, by default, is opened in the viewer pane of your IDE (in case you’re using an IDE that also supports the viewer pane). If a viewer pane is not available, the created HTML output is saved as temporary file and opened in your default web browser. The temporary files are deleted after your R session ends.

Copying table output to office or word processors

Export table as HTML file to open in word processors

You can save the HTML page as file for further usage by specifying the file-argument The saved HTML file can be opened by word processors like LibreOffice or Microsoft Office.

Drag and drop from browser or RStudio viewer pane

You can directly drag and drop a table from the RStudio viewer pane or browser into your word processor. Simply select the complete table with your mouse and drag it into office.

Customizing table output with the CSS parameter

The table output is in in HTML format. The table style (visual appearance) is formatted using Cascading Style Sheets (CSS). If you are a bit familiar with these topics, you can easily customize the appearance of the table output.

Many table elements (header, row, column, cell, summary row, first row or column...) have CSS-class attributes, which can be used to change the table style. Since each sjt.* function as well as tab_model() has different table elements and thus different class attributes, you first need to know which styles can be customized.

Retrieving customizable styles

The table functions invisibly return several values. The return value contains the style information for the HTML table. You can print this style sheet to console using the cat()-function:

m <- lm(barthtot ~ c160age + c12hour + c161sex + c172code, data = efc)
tab <- tab_model(m)

The HTML code is in the page.content return value. The following code prints the HTML code of the table to the R console:


Now you can see which table elements are associated with which CSS class attributes.

Customizing table output with the CSS parameter

You can customize the table output with the CSS parameter. This parameter requires a list of attributes, which follow a certain pattern:

1) each attributes needs a css. prefix 2) followed by the class name (e.g. caption, thead, centeralign, etc.) 3) equal-sign 4) the CSS format (in (single) quotation marks) 5) the CSS format must end with a colon (;)


  CSS = list(
    css.depvarhead = 'color: red;',
    css.centeralign = 'text-align: left;', 
    css.firsttablecol = 'font-weight: bold;', 
    css.summary = 'color: blue;'

In the above example, the header row lost the original style and just became red. If you want to keep the original style and just add additional style information, use the plus-sign (+) as initial character for the parameter attributes. In the following example, the header row keeps its original style and is additionally printed in red:

tab_model(m, CSS = list(css.depvarhead = '+color: red;'))

Pre-defined Table-Layouts

There are a few pre-defined CSS-themes, which can be accessed with the css_theme()-function. There are more pre-defined themes planned for the future.

tab_model(m, CSS = css_theme("cells"))

Try the sjPlot package in your browser

Any scripts or data that you put into this service are public.

sjPlot documentation built on July 10, 2021, 5:07 p.m.