How can I write to Word using the gt package in R without gridlines in the body of the table?

24 Views Asked by At

When I render this code in the RStudio IDE it displays the table I want.

suppressPackageStartupMessages(library(tidyverse))

table2_1 <- 
  data.frame(
    Gender = c("Female", "Female", "Male", "Male"), 
    Belief = c(" Yes", "No", " Yes", "No"),
    Count = c(1230, 357, 859, 413)
  )

`Table 2.1` <- 
  xtabs(Count ~ Gender + Belief, table2_1) |> 
  addmargins() |> 
  as_tibble()

wide <- 
  `Table 2.1` |> 
  pivot_wider(id_cols = Gender, names_from = Belief, values_from = n) |> 
  mutate(
    Gender = if_else(Gender == "Sum", "Total", Gender)
  ) |> 
  rename(
    Yes = ` Yes`,
    `No or Undecided` = No,
    Total = Sum
  )

library(gt)
wide |> 
  gt() |> 
  tab_header(
    title = "Table 2.1 Cross-classification of belief in afterlife by gender."
  ) |> 
  tab_spanner(
    label = "Belief in the Afterlife",
    columns = 2:3
  ) |> 
  cols_align(columns = c("Yes", "No or Undecided"), align = "center") |> 
  tab_source_note(
    source_note = "Source: Data from 2016 General Social Survey."
  ) |> 
tab_style(
  style = cell_borders(
    sides = c("right", "left"),
    style = NULL
  ),
  locations = cells_body()
) |>  
tab_style(
  style = cell_borders(
    sides = c("top", "bottom"),
    color = "white",
    weight = px(1.5),
    style = "solid"
  ),
  locations = cells_body()
) 

When I render it into Word it adds gridlines to the body of the table. How can I get a table that has the border lines (above and below the section with the numbers) but not the gridlines in the body?

To see the problem I am using Quarto:

---
title: "gt Testing"
format: docx
execute: 
  echo: false
---

```{r}
# code goes here
``


0

There are 0 best solutions below