1

I've tried to convert my results got from my paired t.tests into one table to Microsoft Word. However, I found no luck in doing so. Found errors such as 'Error: (converted from warning) package ‘flextable’ was built under R version 4.2.3'. I did update my R recently so I don't see that the program could be out of date.

Here is my code that I done thus far:

##t test for test the effect of the presence of edges on the marks of predation by birds

library(tidyverse)
# Filter out only the "Edge" and "Center" variables in the "Position within the plot"
filtered_data <- subset(Yes, `Position within the plot` %in% c("Edge", "Center"))

print(result <- t.test(`Amount of bird markings per caterpillar` ~ `Position within the plot`, data = filtered_data, paired = TRUE))

##t test for test the effect of the absense of edges on the marks of predation by birds 

# Filter out only the "Edge" and "Center" variables in the "Position within the plot"
filtered_datan <- subset(no, `Position within the plot` %in% c("Edge", "Center"))

print(resultn <- t.test(`Amount of bird markings per caterpillar` ~ `Position within the plot`, data = filtered_datan, paired = TRUE))

I've tried stargazer and tidy from packages such as tidyverse and broom. I even took the long route:

result <- data.frame(
  statistic = result$statistic,
  df = result$parameter,
  p_value = result$p.value,
  conf_int_lower = result$conf.int[1],
  conf_int_upper = result$conf.int[2],
  mean_difference = result$estimate
)

# Export the t-test results to a CSV file
write.csv(result, file = "t_test_results.csv", row.names = FALSE)
tab_model(model, file = "t_test_results.doc", show.se = TRUE, show.std = TRUE, show.stat = TRUE, show.ci = 0.95, show.est = TRUE)

library(flextable)

# Read the CSV file generated from the t-test results
result <- read.csv("t_test_results.csv")

# Convert the data frame to a flextable
ft <- flextable(result)

# Set column names (optional)
colnames(ft) <- c("Statistic", "df", "p-value", "Conf. Int. Lower", "Conf. Int. Upper", "Mean Difference")

  • Your only relevant line appears to be the `tab_model` call, and even that is writing to a long-defunct ".doc" format file? Take a look at the packages `officer` , `doconv` , and `officedown` , perhaps. – Carl Witthoft Aug 01 '23 at 14:26

1 Answers1

0

Several options are available to store a table in a Word (.docx) document using R.

Quarto/RMarkdown

Quarto and RMarkdown documents can be rendered as Word documents. Using knitr::kable() or flextable::flextable() on a data.frame will result in an editable word table.

---
title: "Tables for Word"
format: docx
editor: source
---

## A Simple Table

```{r}
mtcars |> knitr::kable()
```

# A Flextable Table

```{r}
flextable::flextable(mtcars)
```

officer Package

The officer package provides functions for creating Word tables. Here is a simple example:

library(officer)
library(flextable)

ft_mtcars <- flextable(mtcars)

save_as_docx("table 1" = ft_mtcars, path = "table.docx")
Till
  • 3,845
  • 1
  • 11
  • 18