3

Context:

I am using R markdown to produce a short report in Word format. In the R markdown file, I use stargazer to produce summary statistics tables to be included in the final Word document. I'm new to R Markdown. Apologies if this is an elementary question.

The Problem:

When I knit to Word, the stargazer tables show up incorrectly. Each of the words and numbers in the summary statistics table takes up one row and the tables become illegible. The object being summarized is a data frame and necessary packages have been loaded in R Markdown.

What I've Tried:

I've looked at several relevant responses. Some suggest to use kableextra (https://community.rstudio.com/t/nice-tables-when-knitting-to-word/3840/2) and others recommend to export and manually copy a table into Word (https://newbedev.com/using-stargazer-with-rstudio-and-knitr).

I've tried to output into html instead, and the output looks great. In fact, the stargazer tables look fine if I use Word to open the html document knitted from R markdown.

The solutions above all work well as a workaround. However, I am wondering if there's a way to knit directly from R markdown to Word.

Here is an example of the code I've used to produce the tables.

data %>% 
  stargazer(title = "Summary Statistics",
            digits = 0,
            summary = TRUE,
            type = "html")

Any tips would be much appreciated.

Tee
  • 149
  • 8

1 Answers1

0

I am not sure if I understand the initial problem. You say you like to show summaries from stargazer in Word output from markdown. That's working for me.

---
title: "stargazer word"
author: "Me"
date: "2023-01-13"
output:
  word_document: default
  pdf_document: default
  html_document: default
---

## stargazer word

```{r, echo=FALSE, warning=FALSE, message=FALSE}
library(stargazer)

model <- lm(mpg ~ disp + cyl, data=mtcars)

# model summary
stargazer(model, type = "text")

# data summary
stargazer(mtcars, type="text")
```

enter image description here

Marco
  • 2,368
  • 6
  • 22
  • 48