Is there a way to create a table inside a Quarto document without applying the doc's theme to the HTML table.
Usually I use as_raw_html()
to avoid the Quarto style. But in my current use-case, my table contains a custom ggplot which is removed when I use as_raw_html()
.
I've tried using custom output class but this didn't take any effect on the table output.
Reprex
library(tidyverse)
library(gt)
filtered_penguins <- palmerpenguins::penguins |>
filter(!is.na(sex))
penguin_weights <- palmerpenguins::penguins |>
filter(!is.na(sex)) |>
group_by(species) |>
summarise(
Min = min(body_mass_g),
Mean = mean(body_mass_g) |> round(digits = 2),
Max = max(body_mass_g)
) |>
mutate(species = as.character(species), Distribution = species) |>
rename(Species = species)
plot_density_species <- function(species, variable) {
full_range <- filtered_penguins |>
pull({{variable}}) |>
range()
filtered_penguins |>
filter(species == !!species) |>
ggplot(aes(x = {{variable}}, y = species)) +
geom_violin(fill = 'dodgerblue4') +
theme_minimal() +
scale_y_discrete(breaks = NULL) +
scale_x_continuous(breaks = NULL) +
labs(x = element_blank(), y = element_blank()) +
coord_cartesian(xlim = full_range)
}
penguin_weights |>
gt() |>
tab_spanner(
label = 'Penguin\'s Weight',
columns = -Species
) |>
text_transform(
locations = cells_body(columns = 'Distribution'),
# Create a function that takes the a column as input
# and gives a list of ggplots as output
fn = function(column) {
map(column, ~plot_density_species(., body_mass_g)) |>
ggplot_image(height = px(50), aspect_ratio = 3)
}
)
Desired Output
This is the output I get outside of a Quarto document. But inside a Quarto doc, the doc's theme is applied to the table. Using as_raw_html()
removes the plots.