6

I have been using sjplot to create a combined table. This creates a HTML table. I would like to make a table that can be exported to word.

I have reviewed this post which discusses copy and pasting into word, but this alters the formatting of the columns and lines. Output several regression tables into multiple pages of a Word document in R

n1 <- glm(N  ~ Age_2 , data = n_data, family = "binomial")
g1 <- glm(G  ~ Age_2 , data = g1_data, family = "binomial")
ga1 <- glm(G_1  ~ Age_2 , data = ga1_data, family = "binomial")
l1 <- glm(L_1  ~ Age_2 , data = l1_data, family = "binomial")
c1 <- glm(C_1  ~ Age_2 , data = c1_data, family = "binomial")
m1 <- glm(m  ~ Age_2 , data = m1_data, family = "binomial")

tab_model (n1,g1,ga1,l1,c1,m1)

Also is it possible to add a line with the number that had the outcome (ie. number of N), in addition to the total number of observations per group?

Any suggestions? Willing to try other packages.

sar
  • 182
  • 6
  • 26
  • 1
    [`flextable`](https://davidgohel.github.io/flextable/articles/overview.html) outputs to word nicely. I use a combination of `huxtable` and `flextable` when outputing to word (`huxtable` to build the table, `flextable` to output to word) – paqmo Apr 02 '20 at 19:56
  • 2
    the `gtsummary` package might also help here with output to work. You can change the output to knitr instead of gt to render it to word. It also has a way to compare regression models to each other: http://www.danieldsjoberg.com/gtsummary/ – Mike Apr 06 '20 at 13:04
  • 2
    Have you considered rmarkdown and knit to a word doc with knitr::kable for the tables? – Gladwell Apr 08 '20 at 16:42

2 Answers2

3

Since sjPlot outputs to html, it's very hard to get it into a Word document directly. Here's an example of how to do something similar to what you want to do using knitr, rmarkdown, jtools, and huxtable. I'm using RStudio with an rmarkdown document, which I knit to a Word document.

---
title: "jtools to Output Logistic Regression Models"
author: "sar"
date: "`r format(Sys.time(), '%d %B %Y')`"
output: word_document
---

```{r setup, include=FALSE}
library(knitr)
library(jtools)
library(huxtable)

knitr::opts_chunk$set(echo=FALSE, warning = FALSE)

```

# Introduction

This is a test document to demonstrate how knitr and rmarkdown can be used to put output from jtools
into a Word Document

```{r OutputTable}
set.seed(1234)
logistic_s <- data.frame(N=rbinom(200,1,0.5),
                         G=rbinom(200,1,0.5),
                         G_1=rbinom(200,1,0.5),
                         L_1=rbinom(200,1,0.5),
                         C_1=rbinom(200,1,0.5),
                         m=rbinom(200,1,0.5),
                         Age_2=round(rnorm(200,40,6)))

n1 <- glm(N  ~ Age_2 , data = logistic_s, family = "binomial")
g1 <- glm(G  ~ Age_2 , data = logistic_s, family = "binomial")
ga1 <- glm(G_1  ~ Age_2 , data = logistic_s, family = "binomial")
l1 <- glm(L_1  ~ Age_2 , data = logistic_s, family = "binomial")
c1 <- glm(C_1  ~ Age_2 , data = logistic_s, family = "binomial")
m1 <- glm(m  ~ Age_2 , data = logistic_s, family = "binomial")

model_summs <- export_summs(n1,g1,ga1,l1,c1,m1,
                            error_format = "({conf.low}, {conf.high})",
                            model.names = c("N","G","G_1","L_1","C_1","m"))

col_width(model_summs) = c(0.84,rep(0.95,6))

model_summs
```
Sam Dickson
  • 5,082
  • 1
  • 27
  • 45
  • thanks @Sam Dickson. I get this error when displaying the table: Error: pandoc document conversion failed with error 1 In addition: Warning messages: 1: In readLines(con, warn = FALSE) : invalid input found on input connection 'model_summs.doc.Rmd' 2: In knitr::knit(knit_input, knit_output, envir = envir, quiet = quiet) : The file "model_summs.doc.Rmd" must be encoded in UTF-8. Please see https://yihui.org/en/2018/11/biggest-regret-knitr/ for more info. 3: In readLines(con, warn = FALSE) : invalid input found on input connection 'model_summs.doc.knit.md' – sar Apr 11 '20 at 17:29
3

We can do this with the gtsummary package. By default, gtsummary prints tables with the gt packages that does not support Word output. But we can convert any gtsummary object to a type supported by Word. In the example below, we'll convert to a flextable.

I provided 2 formats for your solution: one long table, and one (very) wide table. Here's what the long table looks like: enter image description here

The wide format looks like this:

enter image description here We get the number of events for each model using the add_nevent() function. Here's the full code for the R Markdown file. NOTE: The as_flextable() is new, and you'll need to install the dev version of gtsummary remotes::install_github("ddsjoberg/gtsummary") http://www.danieldsjoberg.com/gtsummary/index.html

---
title: "Regression Tables with gtsummary"
output: word_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Data 

```{r}
set.seed(324524)
logistic_s <- data.frame(N=rbinom(200,1,0.5),
                         G=rbinom(200,1,0.5),
                         G_1=rbinom(200,1,0.5),
                         L_1=rbinom(200,1,0.5),
                         C_1=rbinom(200,1,0.5),
                         m=rbinom(200,1,0.5),
                         Age_2=round(rnorm(200,40,6)))
```

## Long Table

Create a table that is one line per model

```{r}
library(gtsummary)
library(tidyverse)

# build models
tbl_uvregression(
  data = logistic_s,
  x = Age_2,
  method = glm,
  method.args = list(family = binomial),
  exponentiate = TRUE
) %>%
  modify_header(label = "**Model Outcome**") %>%
  # add the number of evenets
  add_nevent() %>%
  # export as flextable instead of gt table
  as_flextable()
```

## Wide Table

Create a table that wide

```{r cars}
# list all outcomes
outcomes <- c("N", "G", "G_1", "L_1", "C_1", "m") 

# map over each outcome to make a model and table
list_regression_tables <- 
  map(outcomes,
      # make a model for each outcome
      ~glm(
        formula = as.formula(paste(.x, "Age_2", sep = "~")),
        data = logistic_s,
        family = binomial
      ) %>%
        # putting model in table with tbl_regression
        tbl_regression(exponentiate = TRUE) %>%
        # add the number of evenets
        add_nevent() 
  )

# merging all tables together
tbl_merge(tbls = list_regression_tables,
          tab_spanner = outcomes) %>%
  # export as flextable instead of gt table
  as_flextable()
```

Happy Coding!

Daniel D. Sjoberg
  • 8,820
  • 2
  • 12
  • 28
  • 1
    thanks @Daniel Sjoberg. I am looking for one model per column (or one model for 3 subcolumns (OR,CI,p). For the rows I am looking to have the model variables. – sar Apr 11 '20 at 17:31
  • 1
    Great, that is what the wide table does. Is that not what you're looking for? (Code in the R markdown chunk) @sar – Daniel D. Sjoberg Apr 11 '20 at 17:33
  • I am getting this error message Error in map(outcomes, ~glm(formula = as.formula(paste(.x, "Age_2", sep = "~")), : could not find function "map" – sar Apr 12 '20 at 17:21
  • I have since altered the models and used individual datasets for each model. I have updated my code in my question. Is there a way to do this with multiple datasets? – sar Apr 13 '20 at 00:00