Table format and output in R
2 answers
Here is one way to use the package htmlTable
install.packages('devtools')
devtools::install_github('gforge/htmlTable')
(you can also find the htmlTable
function in the Gmisc package on cran ( install.packages('Gmisc')
), but it will be removed soon and available in a standalone package called htmlTable
)
out <-
structure(c("37(34%)", "1 (Ref)", "1 (Ref)", "1 (Ref)", "1 (Ref)",
"1 (Ref)", "45(23%)", "0.68 (0.63, 0.73)", "0.38 (0.32, 0.44)",
"0.21 (0.17, 0.28)", "0.08 (0.05, 0.13)", "0.05 (0.02, 0.11)",
"", "0.03", "0.04", "0.03", "0.02", "0.02", "110(34%)", "0.68 (0.65, 0.71)",
"0.38 (0.34, 0.42)", "0.21 (0.18, 0.25)", "0.08 (0.06, 0.11)",
"0.05 (0.03, 0.08)", "", "0.03", "0.04", "0.03", "0.02", "0.02"
),
.Dim = c(6L, 5L),
.Dimnames = list(NULL, c("r", "hr", "p", "hr", "p")))
## format rows/cols
colnames(out) <- c('(n = ***)','HR (92% CI)','P','HR (92% CI)','P')
rownames(out) <- c('PD No (%)','None','Age','Age (<60 vs > 60)',
' Age > 60',' Age < 60')
## add padding row called subset
out <- rbind(out[1:4, ], 'Subsets:' = '', out[5:6, ])
## bolding rownames
rownames(out) <- sprintf('<b>%s</b>', rownames(out))
## table column headers (with line breaks (<br />))
cgroup <- c('', 'R + C<br />(n = ***)', 'R + S<br />(n = ***)')
# zzz <- `rownames<-`(out, NULL)
library(htmlTable)
htmlTable(out, rowlabel = 'Adjustment<sup>†</sup>',
ctable = TRUE, align = 'ccccc',
## number of columns that each cgroup label spans:
n.cgroup = c(1, 2, 2), cgroup = cgroup,
## insert two table spanning sections:
tspanner = c('',''), # no labels
n.tspanner = c(4, 3), # number of rows to span (must sum to nrow(out))
# css.tspanner.sep = "border-bottom: 1px dotted grey;",
caption = "Table 1: Hazard ratios and <i>p</i> values of two models and
something something.",
tfoot = '<font size=1><sup>†</sup>Some note.</font>')
Gives it to me
Moved into problems (headaches) with a dotted line. suggest just using a solid state
+6
source to share