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 ]
# set computed colors
corrFT = setFlexTableBackgroundColors( corrFT,
j = seq_len(ncol(cormatrix)) + 1,
colors = mycolors )
# cosmetics
colors = "gray", to = "header" )
corrFT = setFlexTableBorders( corrFT
)
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 |
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
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
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
, "Gender", "Ulceration", "Count", "Mean (± sd)") )
#create a footer row
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
# format the table grid borders
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 |
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, "< 0.001", formatC( data[, 4], digits=5, format = "f"))
# add signif codes to data
data$Signif = signif.codes
# create an empty FlexTable
, header.columns = T
)
# center the first column and set text as bold italic
# define borders
coef_ft = setFlexTableBorders( coef_ft
)
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 |
*** |
# 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
# Create a FlexTable with data.frame dataset
, body.cell.props = baseCellProp
, header.cell.props = baseCellProp
)
# set columns widths (in inches)
# span successive identical cells within column 1
# overwrites some text formatting properties
# overwrites some paragraph formatting properties
# applies a border grid on table
my_ft = setFlexTableBorders( my_ft
)
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 |