嗨,我正在学习gt表,但是如果我在代码的最后使用%>% gt(),它会将我的df更改为列表。
但是如果我在我的代码之后运行df %>% gt(),它就工作了。
为什么我不能直接通过管道连接到gt()
我的数据来自NFLverse软件包
library(nflfastR)
library(gt)
library(tidyverse)
这将我的df更改为列表,并且不在查看器中显示该表
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()
如果我这样做,它就会工作,查看器会显示我的表
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()
1条答案
按热度按时间t0ybt7op1#
在运行两个代码块中的第一个时,表没有显示在Viewer选项卡中的原因是缺少了指定要打印表的行。这有点像运行
x <- 8
并期望R打印数字8,如果这有意义的话。如果你想将
gt_tbl
对象存储在一个名为pbp_rp
的变量中(这就是你在第一个代码块中所做的),然后打印表,你只需添加一行包含pbp_rp
,如下所示:但是,除非您在代码的多个部分中都需要
gt_tbl
对象,否则像在第二个代码块中所做的那样使用pbp_rp %>% gt()
没有任何错误。但是,如果您只想显示一次表格,而不将其保存以供以后使用,您也可以这样做: