gt
gt copied to clipboard
gt() outputs data, code & settings but no formatted table in Jupyter R notebook.
Using R package gt() example code (from https://gt.rstudio.com/) within Anaconda Jupyter notebook R (4.1.1) environment outputs lots of code but not properly formatted table. Similar issue with other example code.

#
# Note: This sample code is directly from https://gt.rstudio.com/articles/gt-datasets.html
#
# Get vectors of 2-letter country codes for
# each region of Oceania
Australasia <- c("AU", "NZ")
Melanesia <- c("NC", "PG", "SB", "VU")
Micronesia <- c("FM", "GU", "KI", "MH", "MP", "NR", "PW")
Polynesia <- c("PF", "WS", "TO", "TV")
# Create a gt table based on a preprocessed `countrypops`
countrypops %>%
filter(country_code_2 %in% c(
Australasia, Melanesia, Micronesia, Polynesia)
) %>%
filter(year %in% c(1995, 2005, 2015)) %>%
mutate(region = case_when(
country_code_2 %in% Australasia ~ "Australasia",
country_code_2 %in% Melanesia ~ "Melanesia",
country_code_2 %in% Micronesia ~ "Micronesia",
country_code_2 %in% Polynesia ~ "Polynesia",
)) %>%
pivot_wider(names_from = year, values_from = population) %>%
arrange(region, desc(`2015`)) %>%
select(-starts_with("country_code")) %>%
gt(
rowname_col = "country_name",
groupname_col = "region"
) %>%
tab_header(title = "Populations of Oceania's Countries in 1995, 2005, and 2015") %>%
tab_spanner(
label = "Total Population",
columns = c(`1995`, `2005`, `2015`)
) %>%
fmt_number(
columns = c(`1995`, `2005`, `2015`),
decimals = 0,
use_seps = TRUE
)
I experienced the same issue. A quick workaround seems:
- save the gt object in a variable
- run gt:::as.tags.gt_tbl() to print the html table
gt_table = countrypops %>%
gt(
rowname_col = "country_name",
groupname_col = "region"
)
gt:::as.tags.gt_tbl(gt_table)
Thanks for reporting this and sharing the screenshot as well! For sure, gt tables should detect the Jupyter notebook environment and emit HTML. I hope to get this working soon.
Answer above works. I do this to keep piping:
gts <- function(gt_table){
gt:::as.tags.gt_tbl(gt_table)
}
gt(head(mtcars)) %>%
gts()
Hi all, thanks for the solutions @bryanwhiting and @exoulster.
I'm running into a potentially related issue so I'm posting it here instead of opening a new issue.
When I export this R notebook in VSCode (with a gt table cell output) to PDF, instead of printing the table it prints the text Shiny tags cannot be represented in plain text (need html).
I run into this issue whether I use the built-in export to PDF:
...or run the following command from the command line:
jupyter nbconvert --to pdf "gt_table_demo.ipynb"
Setup
# data analysis imports
library(tidyverse)
library(magrittr)
# formatting tables
library(gt)
# color palettes
library(scales)
Create fake data
# create test data
data <- data.frame(
outcome_statement = c(
"This is a very long outcome statement which will require a wide column if it remains in one line",
"This is a second very long outcome statement which will require a wide column if it remains in one line",
"This is a third very long outcome statement which will require a wide column if it remains in one line"),
value_1 = c(1000,2000,3000),
value_2 = c(450000,300,5000),
value_3 = c(8500, 750, 25)
)
Create formatted gt table and view it with gts function
# create gt table output
data %>%
gt() %>%
tab_options(
column_labels.font.weight = "bold",
table.width = pct(100)
) %>%
tab_style(
style = cell_borders(
sides = c("bottom", "right"),
color = "#bfbfbf"
),
locations = cells_body(
columns = everything(),
rows = everything()
)
) %>%
tab_header(
title = "Three Values for Different Outcomes"
) %>%
data_color(
columns = c(
value_1,
value_2,
value_3),
#colors = col_bin(colorRamp(c("#fff8eb", "#fdb734"), interpolate="spline"), domain = c(0,5005), bins = 6)
colors = col_factor(colorRamp(c("#fff8eb", "#fdb734"), interpolate="spline"), domain = NULL)
) %>%
tab_footnote(
footnote = "Description of Value 1",
locations = cells_column_labels(
columns = value_1
)
) %>%
tab_footnote(
footnote = "Description of Value 2",
locations = cells_column_labels(
columns = value_2
)
) %>%
tab_footnote(
footnote = "Description of Value 3",
locations = cells_column_labels(
columns = value_3
)
) %>%
cols_label(
outcome_statement = "Outcome Statement",
value_1 = "Value 1",
value_2 = "Value 2",
value_3 = "Value 3"
) %>%
gts()
Output in Notebook
Output in PDF
If I don't use the gts helper function, here's the output in PDF:
I've found a decent workaround for now: using the weasyprint pdf-engine for pandoc:
pip install weasyprint
pandoc gt_table_demo.ipynb --pdf-engine=weasyprint -o gt_table_demo.pdf
Output PDF contains (mostly correctly) formatted gt table: