r - R 中的表格格式和输出

标签 r r-markdown

谁能给我任何关于如何在 R 中输出此表的提示(链接)?例如,如何将第一行加粗、添加虚线以及为表格制作双标题。我更喜欢 R markdown,rft 也可以,我尽量避免使用 Latex。非常感谢!!

enter image description here

最佳答案

这是使用 htmlTable 包的一种方式

install.packages('devtools')
devtools::install_github('gforge/htmlTable')

(htmlTable函数也可以在cran(install.packages('Gmisc'))的Gmisc包中找到),但很快就会被移除并在名为 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>')

给我这个

enter image description here

遇到虚线的问题(头痛)。建议只使用固体

关于r - R 中的表格格式和输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27712798/

相关文章:

R:强制引入的 Kaggle Titanic 数据集随机森林 NA

r - 使用 roxygen2 导出函数列表

r - 具有不同值标签的 Shiny sliderTextInput 或 sliderInput

r - 在 R Markdown 报告上与经理合作的最佳方式是什么?

css - 如何在 knitr::kable 中连续的表之间保留一些空间?

r - 更改 rmarkdown pdf 中的纸张尺寸和方向

r - 删除向量中的特定元素组?

r - Tidyr::spread() 错误:每行输出必须由唯一的键组合标识

R Markdown : Why Rmarkdown displays no references?

r - 渲染 RMarkdown 文档时修改全局环境