Introduction
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
Method
1. Select your table.
2. Copy the name from Properties > GENERAL > Name.
3. Select Calculation > Custom Code.
4. In the object inspector go to Properties > R CODE.
5. Reference the DT and htmltools R libraries and define the table using the name from step 2:
library(DT)
library(htmltools)
tab = round(original.table, 0)
Here, the table data has also been rounded to 0 decimal places.
6. Set the optional interactive table options as required. In this example, they have all been set to false.
my.options <- list(autoWidth = FALSE,
searching = FALSE,
ordering = FALSE,
lengthChange = FALSE,
lengthMenu = FALSE,
pageLength = FALSE,
paging = FALSE,
info = FALSE)
Leaving all the default options on would display the table as follows:
7. Create an HTML container to store the column header names and set header styling. 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.
header.style <- "th { font-family: 'Arial'; font-weight: bold; color: white; background-color: #008080;}"
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")
)
)
))
8. Use the datatable function to set our table with the above options and header container:
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
9. 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 = " "
. In this example, we have set the background color to teal.
my.table <- formatStyle(my.table,
columns = " ",
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")
10. 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)
. In this example, we use a grey background color and set the cell font to bold teal.
my.table <- formatStyle(my.table,
columns = c(1,2),
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')
11. Add a final line to reference the name of the customized table so that it displays: my.table
Comments
0 comments
Article is closed for comments.