Table format and output in R

Can anyone give me some hints on how to output this table in R (link)? for example, how to make the first line bold, add a dashed line, and make a double header for a table. I prefer R markdown, rft is also ok, I try to avoid Latex. Thank you very much!

enter image description here

+3


source to share


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)',
                   '&emsp;Age > 60','&emsp;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>&dagger;</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>&dagger;</sup>Some note.</font>')

      



Gives it to me

enter image description here

Moved into problems (headaches) with a dotted line. suggest just using a solid state

+6


source


You can use the package ReporteRs

. Below are examples of output and the corresponding R-code here .



You have control over table borders, fonts, paragraphs, etc.

0


source







All Articles