Hi I'm trying to learn gt tables, but if I %>% gt() at the end of my code it changes my df to a list.
But if I after my code run df %>% gt() it works.
Why can't I pipe direct to gt()
my data is from the NFLverse package
library(nflfastR)
library(gt)
library(tidyverse)
this changes my df to a list, and don't show the table in viewer
pbp_rp <- load_pbp(2023) %>%
drop_na(yards_gained) %>%
filter(rush == 1 | pass == 1, !is.na(yards_gained)) %>%
select(posteam, yards_gained, week) %>%
group_by(posteam) %>%
summarise(att = sum(!is.na(yards_gained)),
lost_att = sum(yards_gained <0),
zero_att = sum(yards_gained == "0"),
positive_att = sum(yards_gained >0),
lost_yards = sum(yards_gained [yards_gained<0]),
yards_0_10 = sum(yards_gained [yards_gained>=0 & yards_gained<10]),
yards_10_20 = sum(yards_gained [yards_gained>=10 & yards_gained<20]),
yards_20 = sum(yards_gained [yards_gained>=20]),
positiv_yards = sum(yards_gained [yards_gained>0]),
yards_total = sum(yards_gained),
lower = min(yards_gained),
avg = mean(yards_gained),
upper =max(yards_gained)) %>%
ungroup() %>%
gt()
if I instead do this it works and the viewer shows my table
pbp_rp <- load_pbp(2023) %>%
drop_na(yards_gained) %>%
filter(rush == 1 | pass == 1, !is.na(yards_gained)) %>%
select(posteam, yards_gained, week) %>%
group_by(posteam) %>%
summarise(att = sum(!is.na(yards_gained)),
lost_att = sum(yards_gained <0),
zero_att = sum(yards_gained == "0"),
positive_att = sum(yards_gained >0),
lost_yards = sum(yards_gained [yards_gained<0]),
yards_0_10 = sum(yards_gained [yards_gained>=0 & yards_gained<10]),
yards_10_20 = sum(yards_gained [yards_gained>=10 & yards_gained<20]),
yards_20 = sum(yards_gained [yards_gained>=20]),
positiv_yards = sum(yards_gained [yards_gained>0]),
yards_total = sum(yards_gained),
lower = min(yards_gained),
avg = mean(yards_gained),
upper =max(yards_gained)) %>%
ungroup()
pbp_rp %>% gt()
The reason the table is not shown in the Viewer tab when running the first of your two code chunks is that you're missing a line specifying that you want to print the table. It's a bit like running x <- 8
and expecting R to print the number 8, if that makes sense.
If you want to store the gt_tbl
object in a variable named pbp_rp
(which is what you're doing in the first code chunk) and then print the table, you simply have to add a line containing pbp_rp
like so:
pbp_rp <- load_pbp(2023) %>%
drop_na(yards_gained) %>%
filter(rush == 1 | pass == 1, !is.na(yards_gained)) %>%
select(posteam, yards_gained, week) %>%
group_by(posteam) %>%
summarise(att = sum(!is.na(yards_gained)),
lost_att = sum(yards_gained <0),
zero_att = sum(yards_gained == "0"),
positive_att = sum(yards_gained >0),
lost_yards = sum(yards_gained [yards_gained<0]),
yards_0_10 = sum(yards_gained [yards_gained>=0 & yards_gained<10]),
yards_10_20 = sum(yards_gained [yards_gained>=10 & yards_gained<20]),
yards_20 = sum(yards_gained [yards_gained>=20]),
positiv_yards = sum(yards_gained [yards_gained>0]),
yards_total = sum(yards_gained),
lower = min(yards_gained),
avg = mean(yards_gained),
upper =max(yards_gained)) %>%
ungroup() %>%
gt()
# Display table:
pbp_rp
But unless you need the gt_tbl
object for something in multiple parts of your code, there's nothing wrong in using pbp_rp %>% gt()
like you have done in your second code chunk.
However, if you just want to display the table once without saving it for later use, you could also just do this:
load_pbp(2023) %>%
drop_na(yards_gained) %>%
filter(rush == 1 | pass == 1, !is.na(yards_gained)) %>%
select(posteam, yards_gained, week) %>%
group_by(posteam) %>%
summarise(att = sum(!is.na(yards_gained)),
lost_att = sum(yards_gained <0),
zero_att = sum(yards_gained == "0"),
positive_att = sum(yards_gained >0),
lost_yards = sum(yards_gained [yards_gained<0]),
yards_0_10 = sum(yards_gained [yards_gained>=0 & yards_gained<10]),
yards_10_20 = sum(yards_gained [yards_gained>=10 & yards_gained<20]),
yards_20 = sum(yards_gained [yards_gained>=20]),
positiv_yards = sum(yards_gained [yards_gained>0]),
yards_total = sum(yards_gained),
lower = min(yards_gained),
avg = mean(yards_gained),
upper =max(yards_gained)) %>%
ungroup() %>%
gt()