wjschne / apaquarto

A quarto extension for creating APA7 documents in .docx, .html, and .pdf formats
https://wjschne.github.io/apaquarto/
Creative Commons Zero v1.0 Universal
170 stars 15 forks source link

Tables made with gtsummary #20

Closed klh281 closed 11 months ago

klh281 commented 1 year ago

Thank you for the fantastic extension!

I'd like to include tables made with gtsummary. They don't seem to work properly using the method described in the instructions (i.e., with flextable) -- however, I am trying a work around.

I created a table in another file, then used gtsave to save it as a .png file:

table2 <- 
  mod3 |> tbl_regression(intercept = TRUE) |> 
  modify_header(update = list(label ~ "**Term**", estimate ~ "**Estimate**")) |>
  as_gt() |>
  # tab_header(
  #   title = md("**Table 2**"),
  #   subtitle = md("*Regression estimates and 95% confidence intervals for Model 3*")
  # ) |>
  tab_style(
    style = list(
      cell_text(
        align = "left"
      )
    ),
    locations = list(
      cells_title(groups = "title"),
      cells_title(groups = "subtitle")
    )
  ) |>
  tab_style(
    style = list(
      cell_borders(sides = "top", color = "white", weight = px(2))
    ),
    locations = list(
      cells_title(groups = "title")
    )
  ) |>
  tab_options(table.font.names = "Times New Roman")

table2 |> gtsave("table2.png")

# I'm blocking out the titles since they will be added in the manuscript file.

Then, when creating the manuscript using apaquarto -- I can include the table as follows:

#| apa-cap: Regression estimates and 95% confidence intervals for Model 3
#| apa-note: 
#| ft.align: left

knitr::include_graphics(here::here("programs", "table2.png"))

This approach works well when rendered to .docx -- but will not render to a pdf. For the latter, I get the following error:

compilation failed- error LaTeX Error: Not in outer par mode.

Without the table, the pdf renders just fine.

Do you know of a solution?

Many thanks, Kimberly

wjschne commented 1 year ago

When I tried it, the code inserted a figure inside a table, and latex became confused. Here is a workaround. After creating the table2.png in a plain R code chunk (i.e., no image caption and no apatb label prefix), create an asis code chunk like so:

```{asis}
#| label: apatb-mytable
#| apa-cap: My table caption
#| echo: true

![](table2.png){width=6.5in height=4in}


You may have to adjust the size of the image, the caption, the chunk label, and the folder location of the image.