有没有一种方法可以在R中为Word创建漂亮的表? [英] Is there a way of making beautiful tables for Word in R?

查看:103
本文介绍了有没有一种方法可以在R中为Word创建漂亮的表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SO上问我的问题,这是我最后的希望:-) 我必须编写有关Word的报告. 我在R Markdown上工作,在那里我必须产生许多表. 我希望我的桌子很漂亮!但是我尝试的所有内容(panderKableExtraflextable)均无效.

I ask my question on SO and this is my last hope :-) I have to produce a report on Word. I work on R Markdown where I have to produce many tables. I wish my tables were beautiful! But everything I tried (pander, KableExtra, flextable) did not work.

我从coxph模型中获取结果,对它们进行汇总,然后构造如下所示的data.frame:

I take results from coxphmodels, I aggregate them and then I construct my data.frame which looks like this :

  1                                        Model 1                         Model 2
2                                         n= 375                          n= 374
3                                          e= 65                           e= 64
4                                       PH= 0.46                        PH= 0.97
5            Weight                          ---     1.0  [ 1.0 ; 1.1 ] p = 0.03
6              Size                          --- 1.0  [ 1.0 ; 1.0 ] p = < 10^-3^
7              GR I                          ---                               1
8             GR II                          --- 1.2e+06  [ 0.0 ; Inf ] p = 1.00
9            GR III                          --- 1.4e+06  [ 0.0 ; Inf ] p = 1.00
10            Roads                            1 1.1  [ 1.0 ; 1.1 ] p = < 10^-5^
11            Score  1.0  [ 0.9 ; 1.0 ] p = 0.04                             ---
12 Likelihood ratio Chi-two= 4.48 p-value= 0.034  Chi-two= 2.73 1 p-value= 0.098
                                V4
1                          Model 3
2                           n= 374
3                            e= 64
4                         PH= 0.96
5      1.0  [ 1.0 ; 1.1 ] p = 0.05
6  1.0  [ 1.0 ; 1.0 ] p = < 10^-2^
7                                1
8  1.3e+06  [ 0.0 ; Inf ] p = 1.00
9  1.7e+06  [ 0.0 ; Inf ] p = 1.00
10 1.1  [ 1.1 ; 1.1 ] p = < 10^-6^
11     1.0  [ 0.9 ; 1.0 ] p = 0.10

在Latex上,创建漂亮的表很容易,而我正在使用xtable来获取它 .

On Latex it is easy to create beautiful tables, and I was using xtableto obtain this .

在R Markdown上,我使用print(kable(table)),但不能使用KableExtra中的任何功能来改善外观(编织成文字时不起作用). 而且,我的表是循环制作的,这使情况变得更加困难.

On R Markdown, I use print(kable(table)) but I cannot use any features from KableExtrato improve the presentation (it is not working when knitting to word). Also, my tables are made in a loop making the situation more difficult.

您有任何线索可以从R Markdown在Word上生成此类表格吗?

尤其是:

  • 我们可以去一个单元格里的那一行吗?
  • 我们可以创建多行单元格吗?
  • 我们可以创建多列单元格吗?
  • KableExtra不起作用的情况下,如何添加试条?
  • can we go to the line in a cell ?
  • can we create multirow cell ?
  • can we create multicolumn cell ?
  • how to add strips in my case where KableExtra does not work ?

感谢您对以上问题的回答:-)

Thanks for any answer on the questions above :-)

推荐答案

您是否已经尝试过stargazer软件包?您可以使用type参数指定表的输出格式.编织成文字时,唯一对我有用的是text格式.请参见下面循环运行的示例.它应该运行,但可能需要一些调整.

Did you already try the stargazer package ? You can specify the output format of the table with the type parameter. The only one that works for me when knitting to word is the text format. See the example below that runs in a loop. It should run but may need some tuning.

```{r word_table, comment = ''}
library(stargazer)

lapply(1:3, function(x){
  print(paste("table", x))
  stargazer(attitude, type = 'text')      
})
```

我从以下代码中改编了这段代码: Stargazer输出是代码,而不是桌子

I adapted this piece of code from : Stargazer output is code, not a table

这篇关于有没有一种方法可以在R中为Word创建漂亮的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆