You can create tables in R with more functionality and formatting than what is available with our standard R tables. The DT package is a very powerful package to use to create custom tables, but it requires extensive, more advanced R and HTML coding. You can use the DT package when you want to add specific formatting of your table, search and filtering capabilities, and spark graphs. Some common examples are discussed on RStudio's DT help page. This article describes how to go from a standard table...
...to a customized R table using the DT package.
Requirements
- A table with multiple rows and columns
- Familiarity of R code and HTML is needed as this is advanced code.
- DT has many options so some references to keep handy are:
Method
1. From the toolbar, click Calculation > Custom Code and draw a box on the Page.
2. In the R Code editor and paste in the following code - modify as needed per the comments:
####Load packages and create basic table
#load the required packages of functions
library(DT)
library(htmltools)
#change original.table to the table you'd like to format by clicking on it or pasting in
#the General > Name from the object inspector
tab = original.table
#round the table to 0 digits
tab = round(tab, 0)
3. Add the code below and modify as needed using options in the DT options reference. In this example, they have all been set to false.
####Customize features and interactivity of table
#create a list of options used to format the DT table
my.options <- list(autoWidth = FALSE, #smart width handling
searching = FALSE, #search box above table
ordering = FALSE, #whether columns can be sorted
lengthChange = FALSE, #ability to change number rows shown on page in table
lengthMenu = FALSE, #options lengthChange can be changed to
pageLength = FALSE, #initial number of rows per page of table
paging = FALSE, #whether to do pagination
info = FALSE) #notes whether or not table is filtered
Leaving all the default options as TRUE above instead would display the table as follows:
4. Add the code below and modify as needed. This creates an HTML container to store the column header names and set header styling for the table. In this example, we are pulling the column names from our defined table, tab, and setting the background color to teal and the font color to white.
###Create HTML formatting code for header and overall table HTML container
#create header style HTML code
header.style <- "th { font-family: 'Arial'; font-weight: bold; color: white; background-color: #008080;}"
#pull header names from the table
header.names <- c(" ", colnames(tab))
# The container parameter allows us to design the header of the table using CSS
my.container <- withTags(table(
style(type = "text/css", header.style),
thead(
tr(
lapply(header.names, th, style = "text-align: center; border-right-width: 1px; border-right-style: solid; border-right-color: white; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: white")
)
)
))
5. Add in and modify the code below. Use the datatable function to set our table with the above options and header container:
####Turn the table into a datatable and apply options and HTML formatting from above
my.table = datatable(tab,
options = my.options,
container = my.container,
rownames = TRUE, # Do not treat table row names as separate column
width = '100%', # Ensure table remains within the dimensions of the container
height = '100%') # Ensure table remains within the dimensions of the container
6. Add in and modify the code below. Use the formatStyle function to set the fonts, colors, borders, padding, and alignment of the table row labels by referencing the blank header label as columns = " "
. See many examples on RStudio's gallery. In this example, we have set the background color to teal.
####Create specific table formatting customizations for row labels of table
my.table <- formatStyle(my.table,
columns = " ", #blank columns means row labels
backgroundColor = "#008080",
borderBottomColor = "#ffffff",
borderBottomStyle = "solid",
borderBottomWidth = "1px",
borderCollapse = "collapse",
borderRightColor = "#ffffff",
borderRightStyle = "solid",
borderRightWidth = "1px",
color = "#ffffff",
fontFamily = "Arial",
fontSize = "13px",
fontWeight = "bold",
lineHeight = "normal",
paddingBottom = "2.6px",
paddingLeft = "5.2px",
paddingRight = "5.2px",
paddingTop = "2.6px",
textAlign = "left",
verticalAlign = "middle")
7. Use the formatStyle function to set the fonts, colors, borders, padding, and alignment of the table cells in the 2 columns by referencing columns = c(1,2)
. See many examples on RStudio's gallery. In this example, we use a grey background color and set the cell font to bold teal.
####Create specific table formatting customizations for first two columns of table
my.table <- formatStyle(my.table,
columns = c(1,2), #specify columns to format
fontFamily = "Arial",
fontSize = "16px",
color = '#008080',
fontWeight = 'bold',
paddingRight = "1em",
borderRightWidth = "1px",
borderRightStyle = "solid",
borderRightColor = "white",
borderBottomColor = "#ffffff",
borderBottomStyle = "solid",
borderBottomWidth = "1px",
borderCollapse = "collapse",
verticalAlign = "middle",
textAlign = "center",
wordWrap = "break-word",
backgroundColor = '#e6e6e5')
8. Add a final line to reference the name of the customized table so that it displays the finished table:
###show final finished table
my.table