Correlation matrix

data( mtcars )
cormatrix = cor(mtcars)
pal = c( "#D73027", "#F46D43", "#FDAE61", "#FEE08B",
"#D9EF8B", "#A6D96A", "#66BD63", "#1A9850" )
mycut = cut( cormatrix,
breaks = c(-1,-0.75,-0.5,-0.25,0,0.25,0.5,0.75,1),
include.lowest = TRUE, label = FALSE )
mycolors = pal[ mycut ]
corrFT = FlexTable( round(cormatrix, 3), add.rownames = TRUE )
# set computed colors
corrFT = setFlexTableBackgroundColors( corrFT,
j = seq_len(ncol(cormatrix)) + 1,
colors = mycolors )
# cosmetics
corrFT = setFlexTableBackgroundColors( corrFT, i = 1,
colors = "gray", to = "header" )
corrFT[1, , to = "header"] = textBold(color="white")
corrFT = setFlexTableBackgroundColors( corrFT, j = 1, colors = "gray" )
corrFT[,1] = textBold(color="white")
corrFT = setFlexTableBorders( corrFT
, inner.vertical = borderProperties( style = "dashed", color = "white" )
, inner.horizontal = borderProperties( style = "dashed", color = "white" )
, outer.vertical = borderProperties( width = 2, color = "white" )
, outer.horizontal = borderProperties( width = 2, color = "white" )
)
corrFT

mpg

cyl

disp

hp

drat

wt

qsec

vs

am

gear

carb

mpg

1.000

-0.852

-0.848

-0.776

0.681

-0.868

0.419

0.664

0.600

0.480

-0.551

cyl

-0.852

1.000

0.902

0.832

-0.700

0.782

-0.591

-0.811

-0.523

-0.493

0.527

disp

-0.848

0.902

1.000

0.791

-0.710

0.888

-0.434

-0.710

-0.591

-0.556

0.395

hp

-0.776

0.832

0.791

1.000

-0.449

0.659

-0.708

-0.723

-0.243

-0.126

0.750

drat

0.681

-0.700

-0.710

-0.449

1.000

-0.712

0.091

0.440

0.713

0.700

-0.091

wt

-0.868

0.782

0.888

0.659

-0.712

1.000

-0.175

-0.555

-0.692

-0.583

0.428

qsec

0.419

-0.591

-0.434

-0.708

0.091

-0.175

1.000

0.745

-0.230

-0.213

-0.656

vs

0.664

-0.811

-0.710

-0.723

0.440

-0.555

0.745

1.000

0.168

0.206

-0.570

am

0.600

-0.523

-0.591

-0.243

0.713

-0.692

-0.230

0.168

1.000

0.794

0.058

gear

0.480

-0.493

-0.556

-0.126

0.700

-0.583

-0.213

0.206

0.794

1.000

0.274

carb

-0.551

0.527

0.395

0.750

-0.091

0.428

-0.656

-0.570

0.058

0.274

1.000

Clinical like table

Example 1

data = structure(list(Status = c("Alive", "Alive", "Alive", "Alive",
"Melanoma", "Melanoma", "Melanoma", "Melanoma", "Non-melanoma",
"Non-melanoma", "Non-melanoma", "Non-melanoma"), Gender = c("Female",
"Female", "Male", "Male", "Female", "Female", "Male", "Male",
"Female", "Female", "Male", "Male"), Ulceration = c("Absent",
"Present", "Absent", "Present", "Absent", "Present", "Absent",
"Present", "Absent", "Present", "Absent", "Present"), n = c(68L,
23L, 24L, 19L, 8L, 20L, 8L, 21L, 3L, 4L, 4L, 3L), Mean = c(1.693,
2.972, 1.468, 4.319, 2.139, 4.724, 3.266, 5.143, 1.667, 3.302,
2.42, 8.053), SD = c(2.004, 2.593, 1.719, 2.423, 1.184, 4.128,
4.681, 2.862, 1.141, 3.713, 2.499, 4.019)), class = "data.frame", .Names = c("Status",
"Gender", "Ulceration", "n", "Mean", "SD"), row.names = c(NA,
-12L))
data
##          Status Gender Ulceration  n  Mean    SD
## 1         Alive Female     Absent 68 1.693 2.004
## 2         Alive Female    Present 23 2.972 2.593
## 3         Alive   Male     Absent 24 1.468 1.719
## 4         Alive   Male    Present 19 4.319 2.423
## 5      Melanoma Female     Absent  8 2.139 1.184
## 6      Melanoma Female    Present 20 4.724 4.128
## 7      Melanoma   Male     Absent  8 3.266 4.681
## 8      Melanoma   Male    Present 21 5.143 2.862
## 9  Non-melanoma Female     Absent  3 1.667 1.141
## 10 Non-melanoma Female    Present  4 3.302 3.713
## 11 Non-melanoma   Male     Absent  4 2.420 2.499
## 12 Non-melanoma   Male    Present  3 8.053 4.019
# create a FlexTable
# do not print header column names
MyFTable = FlexTable( data = data[ , c( "Status", "Gender", "Ulceration", "n", "Mean") ],
header.columns = FALSE )
MyFTable

Alive

Female

Absent

68

1.693

Alive

Female

Present

23

2.972

Alive

Male

Absent

24

1.468

Alive

Male

Present

19

4.319

Melanoma

Female

Absent

8

2.139

Melanoma

Female

Present

20

4.724

Melanoma

Male

Absent

8

3.266

Melanoma

Male

Present

21

5.143

Non-melanoma

Female

Absent

3

1.667

Non-melanoma

Female

Present

4

3.302

Non-melanoma

Male

Absent

4

2.420

Non-melanoma

Male

Present

3

8.053

# span rows of columns Status and Gender
MyFTable = spanFlexTableRows( MyFTable, j = "Status", runs = as.character( data$Status ) )
MyFTable = spanFlexTableRows( MyFTable, j = "Gender", runs = as.character( data$Gender ) )
MyFTable

Alive

Female

Absent

68

1.693

Present

23

2.972

Male

Absent

24

1.468

Present

19

4.319

Melanoma

Female

Absent

8

2.139

Present

20

4.724

Male

Absent

8

3.266

Present

21

5.143

Non-melanoma

Female

Absent

3

1.667

Present

4

3.302

Male

Absent

4

2.420

Present

3

8.053

# add "(sd)" to column 5
MyFTable[, "Mean"] = paste0( " (±", formatC( data$SD, digits=3, format = "f"), ")" )
MyFTable

Alive

Female

Absent

68

1.693 (±2.004)

Present

23

2.972 (±2.593)

Male

Absent

24

1.468 (±1.719)

Present

19

4.319 (±2.423)

Melanoma

Female

Absent

8

2.139 (±1.184)

Present

20

4.724 (±4.128)

Male

Absent

8

3.266 (±4.681)

Present

21

5.143 (±2.862)

Non-melanoma

Female

Absent

3

1.667 (±1.141)

Present

4

3.302 (±3.713)

Male

Absent

4

2.420 (±2.499)

Present

3

8.053 (±4.019)

#create a first header row
MyFTable = addHeaderRow( MyFTable,
value = c("", "Summary statistics for thickness"), colspan = c( 3, 2 ) )
#create a second header row with names
MyFTable = addHeaderRow(MyFTable, value = c( "Status"
, "Gender", "Ulceration", "Count", "Mean (± sd)") )
#create a footer row
MyFTable = addFooterRow( MyFTable, value = c("* This is a footer note"), colspan = 5 )
MyFTable[2,5, to="header"] = pot("*", textProperties( vertical.align = "superscript" ) )
MyFTable

Summary statistics for thickness

Status

Gender

Ulceration

Count

Mean (± sd)*

Alive

Female

Absent

68

1.693 (±2.004)

Present

23

2.972 (±2.593)

Male

Absent

24

1.468 (±1.719)

Present

19

4.319 (±2.423)

Melanoma

Female

Absent

8

2.139 (±1.184)

Present

20

4.724 (±4.128)

Male

Absent

8

3.266 (±4.681)

Present

21

5.143 (±2.862)

Non-melanoma

Female

Absent

3

1.667 (±1.141)

Present

4

3.302 (±3.713)

Male

Absent

4

2.420 (±2.499)

Present

3

8.053 (±4.019)

* This is a footer note

# border properties to use later
no_border = borderProperties( width = 0 )
big_border = borderProperties( width = 2 )
std_border = borderProperties( width = 1 )
another_border = borderProperties(width = 2, color = "red", style = "dashed")
# format the table grid borders
MyFTable = setFlexTableBorders( MyFTable, footer = TRUE,
inner.vertical = no_border, inner.horizontal = std_border,
outer.vertical = no_border, outer.horizontal = big_border )
MyFTable[2,1:3, to="header", side = "top"] = no_border
MyFTable

Summary statistics for thickness

Status

Gender

Ulceration

Count

Mean (± sd)*

Alive

Female

Absent

68

1.693 (±2.004)

Present

23

2.972 (±2.593)

Male

Absent

24

1.468 (±1.719)

Present

19

4.319 (±2.423)

Melanoma

Female

Absent

8

2.139 (±1.184)

Present

20

4.724 (±4.128)

Male

Absent

8

3.266 (±4.681)

Present

21

5.143 (±2.862)

Non-melanoma

Female

Absent

3

1.667 (±1.141)

Present

4

3.302 (±3.713)

Male

Absent

4

2.420 (±2.499)

Present

3

8.053 (±4.019)

* This is a footer note

Model summary table

data(mtcars)
mtcars$cyl = as.factor(mtcars$cyl)
data = summary(lm(mpg ~ wt + cyl, data=mtcars))$coefficients
data = as.data.frame( data )
# get signif codes
signif.codes = cut( data[,4]
, breaks = c( -Inf, 0.001, 0.01, 0.05, Inf)
, labels= c("***", "**", "*", "" ) )
# format the data values
data[, 1] = formatC( data[, 1], digits=3, format = "f")
data[, 2] = formatC( data[, 2], digits=3, format = "f")
data[, 3] = formatC( data[, 3], digits=3, format = "f")
data[, 4] = ifelse( data[, 4] < 0.001, "&lt; 0.001", formatC( data[, 4], digits=5, format = "f"))
# add signif codes to data
data$Signif = signif.codes
# create an empty FlexTable
coef_ft = FlexTable( data = data, add.rownames=TRUE
, body.par.props = parRight(), header.text.props = textBold()
, header.columns = T
)
# center the first column and set text as bold italic
coef_ft[,1] = parCenter()
coef_ft[,1] = textBoldItalic()
# define borders
coef_ft = setFlexTableBorders( coef_ft
, inner.vertical = borderNone(), inner.horizontal = borderDotted()
, outer.vertical = borderNone(), outer.horizontal = borderSolid()
)
coef_ft

Estimate

Std. Error

t value

Pr(>|t|)

Signif

(Intercept)

33.991

1.888

18.006

< 0.001

***

wt

-3.206

0.754

-4.252

< 0.001

***

cyl6

-4.256

1.386

-3.070

0.00472

**

cyl8

-6.071

1.652

-3.674

< 0.001

***

Conditional colored table

# a summary of mtcars
dataset = aggregate( mtcars[, c("disp", "mpg", "wt")]
, by = mtcars[, c("cyl", "gear", "carb")]
, FUN = mean )
dataset = dataset[ order(dataset$cyl, dataset$gear, dataset$carb), ]
# set cell padding defaut to 2
baseCellProp = cellProperties( padding = 2 )
# Create a FlexTable with data.frame dataset
my_ft = FlexTable( data = dataset
, body.cell.props = baseCellProp
, header.cell.props = baseCellProp
, header.par.props = parProperties(text.align = "center" )
)
# set columns widths (in inches)
my_ft = setFlexTableWidths( my_ft, widths = c(0.5, 0.5, 0.5, 0.7, 0.7, 0.7) )
# span successive identical cells within column 1
my_ft = spanFlexTableRows( my_ft, j = 1, runs = as.character( dataset$cyl ) )
# overwrites some text formatting properties
my_ft[dataset$wt < 3, 6] = textProperties( color="#FF3333", font.weight = "bold" )
my_ft[dataset$mpg < 20, 5] = chprop( baseCellProp, background.color = "#00AAAA")
# overwrites some paragraph formatting properties
my_ft[, 4:6] = parProperties(text.align = "right")
# applies a border grid on table
my_ft = setFlexTableBorders( my_ft
, outer.vertical = borderProperties( width = 2 )
, outer.horizontal = borderProperties( width = 2 )
)
my_ft

cyl

gear

carb

disp

mpg

wt

4

3

1

120.10

21.50

2.46500

4

1

84.20

29.10

2.07250

4

2

121.05

24.75

2.68375

5

2

107.70

28.20

1.82650

6

3

1

241.50

19.75

3.33750

4

4

163.80

19.75

3.09375

5

6

145.00

19.70

2.77000

8

3

2

345.50

17.15

3.56000

3

3

275.80

16.30

3.86000

3

4

416.40

12.62

4.68580

5

4

351.00

15.80

3.17000

5

8

301.00

15.00

3.57000