I have a simple quarto markdown which generates table using gt
package from a data frame.
Example:
---
title: "Untitled"
format: pdf
editor: visual
---
```
```{r,include = FALSE, echo = FALSE}
library(gt)
library(tidyverse)
```
```{r, include = FALSE}
intro <- data.frame("Name" = "Python",
"Age" = "33",
"Height" = "14")
```
When I use the pivot_longer()
, name
and value
column header gets generated. So to remove that I have used column_labels.hidden = TRUE
.
```{r, echo = FALSE}
intro %>%
pivot_longer(cols = everything()) %>%
gt() %>%
tab_options(column_labels.hidden = TRUE) %>%
tab_style(
style = cell_text(weight = "bold"),
locations = cells_column_labels(columns = 1)
) %>%
tab_style(
style = cell_text(weight = "bold"),
locations = cells_body(columns = 1)
)
```
When I run this in the code chunk it works as expected showing without the column header and the desired cell text is in bold.
Problem:
When I render this, all the format gets lost. Even the name
and value
shows up in the final table.
Thanks for the help.
Alternative way using flextable
package.
std_border = fp_border(color="black")
ft <- intro %>%
pivot_longer(cols = everything()) %>%
flextable()
ft <- ft |>
delete_part( part = "header") |>
border_remove() |>
hline( part="all", border = std_border ) |>
vline( border = std_border ) |>
vline_left(border = std_border ) |>
hline_top(part="all", border = std_border)|>
autofit()
ft