Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way of making beautiful tables for Word in R?

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.

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

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

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.

Do you have any clue to produce such table on Word from R Markdown ?

In particular :

  • 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 :-)

like image 784
Flora Grappelli Avatar asked Sep 26 '19 09:09

Flora Grappelli


2 Answers

kableExtra has its focus on tables for html content. However, I sometimes also use it to create tables for a Word document via previous html output. First, I set up the specs for the table in kableExtra(see documentation; important: skip the html attribute in the kableExtra specs, it makes subsequent copying of tables to Word difficult). Then I just knit the corresponding RMarkdown document to html just using results='show' (I am not using print in these cases). From the resulting html document you should be able to copy and paste the tables into Word. Did you try this? I assume that the print command does not allow the usual kableExtra table styling. Could you also loop over your tables using lapply?

like image 110
GRowInG Avatar answered Nov 10 '22 16:11

GRowInG


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')      
})
```

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

like image 25
Paul Endymion Avatar answered Nov 10 '22 16:11

Paul Endymion