This article describes how to customize the fonts shown in CreateCustomTable R Tables. The article explains the steps required to go from a standard table looking like this...
...to a custom table with specific fonts used for different parts of the table looking like this:
And also a table where the font dynamically changes based on the data shown...
Requirements
- A table of data to format. Click here to get a copy of the examples below (and more) in your Displayr account.
- Familiarity with the settings you can pass to the CreateCustomTable function.
Method - Customizing Fonts for Parts of a Table
In the example below, I'm creating a custom table similar to our built-in Autofit tables using the CreateCustomTable function in R.
I'm specifying a different font type for each of the different parts of the table, e.g. column, and row headers. To modify the below code for your own document, please read through the comments (denoted with #) and edit where appropriate. You can see other options for customizing fonts at CreateCustomTable.
- From the toolbar, click Calculation > Custom Code and draw a box for your calculation on the page.
- Paste the following R code in the R code editor and click Calculate:
#specify which table you want to format
mytable = table.Income.5
#create custom table
flipFormat::CreateCustomTable(mytable,
font.size=8,
col.header.font.size=15,
row.header.font.style="italic",
row.header.font.color="steelblue")
Method - Customizing Fonts Based on Data in a Table
In the example below, I'm creating a custom table similar to our built-in Autofit tables using the CreateCustomTable function in R. You may want to review the Matrix section of How to Work with Data in R to help understand what the code is doing.
I'm specifying a different font for rows with a small sample size and the row headers of rows with the highest values. To modify your own document, please read through the comments (denoted with #) and edit where appropriate. You can see other options for customizing the font at CreateCustomTable.
- From the toolbar, click Calculation > Custom Code and draw a box for your calculation on the Page.
- Paste the following R code in the R code editor and click Calculate:
#specify which table you want to format
mytable = table.Income.5
###set the cell font colors
#make matrix of cell.colors the same shape as your table
#default the color to black
cell.font.colors=matrix("black",nrow=NROW(mytable),ncol=NCOL(mytable))
#shade cells with low counts in the second column
cell.font.colors[mytable[,2] < 20,2]="red"
###set the font sizes
#create matrix of default font sizes to 12
cell.font.sizes=matrix(12,nrow=NROW(mytable),ncol=NCOL(mytable))
#increase the font where the first column is over 15 to highlight
cell.font.sizes[mytable[1:NROW(mytable)-1,1] > 15,1]=15
###set the font weights
#create matrix of font weights default to unbolded
cell.font.weights=matrix("normal",nrow=NROW(mytable),ncol=NCOL(mytable))
#bold the data where it's over 15
cell.font.weights[mytable[1:NROW(mytable)-1,1] > 15,1]="bold"
###set the row header colors
#create list of colors defaulting to black
row.font.colors=rep("black",length(rownames(mytable)))
#where the data is over 15 change the row header to red
row.font.colors[mytable[1:NROW(mytable)-1,1] > 15]="red"
#create custom table
flipFormat::CreateCustomTable(mytable,
col.widths=c(40,30,30),
cell.font.color=cell.font.colors,
cell.font.size=cell.font.sizes,
cell.font.weight=cell.font.weights,
row.header.font.color=row.font.colors)
Next
How to Customize Colors on a CreateCustomTable R Table
How to Add Statistical Significance to CreateCustomTable R Tables
How to Work with Conditional R Formulas
How to Use Different Types of Data in R (if-else statements)