7

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

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Flora Grappelli
  • 659
  • 9
  • 26
  • Possible duplicate of [How can I print a table in R with ascii, html, or markdown formatting?](https://stackoverflow.com/questions/15849043/how-can-i-print-a-table-in-r-with-ascii-html-or-markdown-formatting) – Paul Endymion Sep 26 '19 at 09:12
  • I tried this method without success ! My tables are produced in a loop, so I have to use the `print` function that changes the results. – Flora Grappelli Sep 26 '19 at 09:20
  • I found that knitting to Word works less well in many cases than knitting to html and then copy pasting everything to Word, as Word (sometimes) translates HTML documents fairly decently. Obviously probably not a satisfactory solution to your problem and still involves a lot of editing in Word. – yoland Sep 26 '19 at 09:48
  • You could try the `huxreg()` function from `huxtable` (my package). It uses `flextable` under the hood to print stuff out to Word. You can indeed create multirow and multicolumn cells. – dash2 Oct 03 '19 at 03:52
  • @dash2 thanks for the suggestion! `flextable`does the job :-) I spent the all day yesterday working with it. It seems adapted. At the begining I wanted to use `kableExtra` but `flextable`seems better to do this kind of tasks – Flora Grappelli Oct 03 '19 at 08:16

2 Answers2

1

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?

GRowInG
  • 357
  • 3
  • 14
  • Thanks for the answer! When using `kableExtra` in a loop, no results are shown in Word or Html without the `print` which removes all the styling. Even with `results='show'`. I will test it with `lapply` but my hopes are low – Flora Grappelli Sep 26 '19 at 16:54
  • I made a try with `lapply`. It's not simple because I have two arguments in my function. And `mapply` does notproduce the wanted output. I think I'll have to tinker. – Flora Grappelli Sep 27 '19 at 09:21
  • Do you want to edit / improve your answer or do you want me to suggest one based on your answer? – Flora Grappelli Sep 27 '19 at 09:22
  • 1
    Maybe we don't need to reinvent the wheel using ```lapply```. I've just seen that a possible solution has already been posted which allows using print and ```kable_styling```: [https://stackoverflow.com/questions/51696689/for-loop-for-creating-multiple-tables-using-knitr-and-kableextra-packages-in-rma] This example is for a pdf output (and may even work when knitting to Word) but should also work for html when using ```"html"``` instead of ```"latex"```. But maybe someone also reading your post has already done this using ```lapply```, too. – GRowInG Sep 28 '19 at 11:50
  • It is exactly what I used at the end ! Thanks a lot for all the suggestions :-) – Flora Grappelli Oct 03 '19 at 08:19
1

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

Paul Endymion
  • 537
  • 3
  • 18