2014-12-30 45 views
3

任何人都可以给我任何提示如何在R(链接)输出此表?例如,如何加粗第一行,添加虚线以及为表格创建双重标题。我更喜欢R降价,RFT也行,我试图避免乳胶。非常感谢!!表格格式和输出在R

enter image description here

+0

你肯定不能用markdown做到这一点。使用LaTeX或HTML,虽然这条虚线似乎并不是我广泛使用的标准。无论如何,如果你希望得到任何答案,例如你的数据/模型,你应该想出一个最小可重现的例子? – daroczig

回答

6

下面是使用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

然与虚线问题(头痛)。建议只使用固体

+0

这真棒!非常感谢你! –

+0

它工作得很好!输出是一个HTML页面,有什么办法将其转换/输出到一个Word文件? –

+0

我通常在html/latex中保留所有东西,当我需要的时候(hackily)将其放入单词中(因为我经常不足以找到更好的方法;而且我还没有找到从html /乳胶到词很容易)。还有其他一些选择:1)[带有示例的pandoc](http://johnmacfarlane.net/pandoc/demos.html); 2)在.rmd文件的顶部使用正确的[yaml front matter](http://rmarkdown.rstudio.com/word_document_format.html); 3)(最适合我的)复制/粘贴到word/excel中,并在需要时进行编辑(通常在rmd中完成后比较容易添加) – rawr

0

你可以使用包ReporteRs。有输出示例和相应的R代码here

你可以控制表格边框,字体,段落等。

+0

谢谢!我会尝试一下 –