-
I'm writing a Quarto document where I mix R, Python, and SQL code chunks. In each language I end up printing a table of data. However, tables are rendered differently. Is there a way to set a global rule to ensure that all tables, regardless of language, will be in the same format/aesthetics? In other words, is this a feature Quarto already has? I did use
but it seems to affect only tables resulting from R code chunks. The Python table ( And the SQL table is just a thing of its own, although somewhat similar to R's table: Is there a way to make all tables be rendered in the same format/style?
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
Write or use something that produces the same HTML/CSS output, or leave all the styling to Quarto by "printing" markdown tables. To note, Quarto cannot standardise outputs of nearly infinity of functions designed to output tables (or anything), e.g., in R, there is already |
Beta Was this translation helpful? Give feedback.
-
I found a It is not perfect. It relies on an additional R chunk to print the dataframe/tibble/python-DataFrame/SQL-table. But it does work and it gives a consistent look to all tables in the ```{r} #| echo: false library(reticulate) use_virtualenv("./env", required = TRUE) ``` ## Set up SQL ```{r} # R library(DBI) library(duckdb) library(ggplot2) ``` ```{r} # R ## create connection to SQL database con <- DBI::dbConnect(duckdb::duckdb(), dbdir = "duckdb") ## copy mpg dataset from ggplot2 onto the SQL database, via `con` dbWriteTable(con, "mpg", ggplot2::mpg, overwrite = TRUE) ``` ## Print tables from R, Python, and SQL in panel tabset ```{r} #| echo: false # display NaN cells in DT::datatable() options(htmlwidgets.TOJSON_ARGS = list(na = 'string')) # https://stackoverflow.com/a/48694410/6105259 print_as_dt <- function(x){ DT::datatable(x, rownames = FALSE, options = list( dom = 'ltipr', # to remove search box; https://stackoverflow.com/a/61952067/6105259 autoWidth = TRUE, columnDefs = list( list(className = 'dt-left', targets = "_all"), list(width = '200px', targets = "_all") ) ) ) } ``` ::: {.panel-tabset} ## R ```{r} ggplot2::mpg |> dplyr::select(manufacturer, displ, cty) |> print_as_dt() ``` ## Python ```{python} import pandas as pd import numpy as np x = [1,2,np.nan] y = [3,4,5] df = pd.DataFrame({'x':x,'y':y}) ``` ```{r} #| echo: false print_as_dt(py$df) ``` ## SQL ```{sql} #| connection: con #| output.var: "tab_sql" SELECT manufacturer, displ, cty FROM mpg; ``` ```{r} #| echo: false print_as_dt(tab_sql) ``` ::: |
Beta Was this translation helpful? Give feedback.
-
That's a clever workaround, I like it! Quarto currently doesn't have such a feature. We'll be working on it in the next 3-6 months. In the meantime, the issue is that we defer the styling of the tables to individual libraries, and different libraries tend to make different choices. We want to design something that allows libraries the option to control formatting, while also providing an option for quarto to take over formatting. Still, I like your idea of serializing the table and sending it through your preferred library while we figure this all out. |
Beta Was this translation helpful? Give feedback.
I found a
DT::datatable()
solution based on this issue comment.It is not perfect. It relies on an additional R chunk to print the dataframe/tibble/python-DataFrame/SQL-table. But it does work and it gives a consistent look to all tables in the
.qmd
-> HTML page, regardless of language.