3

I created a summary table of my models in R with the package modelsummary and then customized the look with the kableExtra package.

tab<-modelsummary(welfarestate,stars=c("*"=.1,"**"=.05,"***"=.01),statistic="std.error",title="WVS Welfarestate",output = "kableExtra",notes=list("*p<0.1, **p<0.05, ***p<0.1"),add_rows = rows_welfarestate)

tab%>%kable_styling(c("striped","hover"),font_size = 20)

What ist the best way to integrate the output in the R Viewer window into a Word document without losing the formatting?

I found the information that it should be possible to copy the html output generated by kableExtra from the Viewer in R directly into a Word document. However,when I simply copy and paste nothing is displayed in my Word document. And copying from the html output in a web browser doesnt keep the formattig. Just creating a png loses some quality somehow.

Thanks in advance for your help.

jsmill
  • 31
  • 2

1 Answers1

2

kableExtra does not support output to Word documents. As you noted, we can save the table to an HTML file, either with modelsummary()'s output argument, or with kableExtra's save_kable() function. Then, you can open the document in Word, but you will almost certainly lose special formatting and styling.

In my view, if you need a Word table, the best option is to use flextable output instead of kableExtra. In modelsummary you can do that by saving to a .docx file directly:

library(modelsummary)
library(flextable)
mod <- lm(mpg ~ hp, data = mtcars)

modelsummary(mod, output = "table.docx")

Alternatively, you can use one of the many flextable customization functions. For example:

modelsummary(mod, output = "flextable") |>
    theme_zebra() |>
    save_as_docx(path = "table.docx")

Note that flextable is super flexible. See their documentation: https://ardata-fr.github.io/flextable-book/

Vincent
  • 15,809
  • 7
  • 37
  • 39
  • 2
    Thanks for your answer! I will have a closer look at the `flextable` package. I was just confused because the option of copy/paste from `R` to Word and keep formatting (even without creating a docx file) is even mentioned in the official documentation site: https://haozhu233.github.io/kableExtra/ in the section "kabelExtra and Word". But if `flextable` allows me to customize the table and save it as a docx file, without having to do too much post formatting in Word, that should be enough. – jsmill Jul 26 '22 at 14:44
  • I've personally not had much success with this HTML to Word strategy, but your mileage may vary. The video you link to looks compelling... – Vincent Jul 26 '22 at 17:15
  • It looks super compelling. I even saw a person mentioning in another forum that it worked for them. But somehow it doesnt in my case (maybe I should ask the package creator). As exporting a png from the R viewer gives me the exact output I want to have and is easy to integrate into Word, you dont coincidentilly know how to avoid the slight quality loss of this procedure? – jsmill Jul 26 '22 at 17:36
  • No, sorry. I've tried all the options that seemed vaguely DPI-related but couldn't figure this out. – Vincent Jul 26 '22 at 17:39
  • 1
    Ok, thanks anyway. I guess I have to switch to R Markdown/Latex at one point as things seem to be much more easy to integrate there. – jsmill Jul 26 '22 at 17:48