简体   繁体   English

如何让 gt 表与 get_summary_stats 一起使用

[英]How do I get gt table to work with get_summary_stats

Dataset数据集

Here is the dput head for my dataset:这是我的数据集的输入头:

structure(list(Year = c("2021", "2021", "2021", "2021", "2021", 
"2021"), Month_Number = c("9", "9", "9", "9", "9", "9"), Month_Name = c("September", 
"September", "September", "September", "September", "September"
), Day_Name = c("Wednesday", "Thursday", "Friday", "Saturday", 
"Sunday", "Monday"), Time_Wake = c(500L, 715L, 600L, 600L, 700L, 
600L), Mins_Sleep = c(300L, 540L, 540L, 480L, 480L, 480L), Start_Work = c(1015L, 
1000L, 945L, 1400L, 1500L, 915L), End_Work = c(1800L, 1600L, 
1210L, 1700L, 1515L, 1530L), Workout_Y_N = c("Y", "Y", "Y", "N", 
"N", "N"), Time_Workout = c(730L, 730L, 730L, NA, NA, NA), Work_Environment = c("Office", 
"Office", "Office", "Home", "Home", "Office"), Coffee_Cups = c(3L, 
0L, 2L, 6L, 4L, 5L), Tea_Cups = c(2L, 4L, 2L, 0L, 0L, 2L), Mins_Work = c(435L, 
350L, 145L, 135L, 15L, 60L)), row.names = c(NA, 6L), class = "data.frame")

Script脚本

I'm trying to run a table on my summary stats but having an issue with using the gt library to produce it.我正在尝试在我的摘要统计信息上运行一个表格,但在使用 gt 库生成它时遇到问题。 Here is my script:这是我的脚本:

library(tidyverse)
library(gt)
library(rstatix)

work %>% 
  get_summary_stats() %>% 
  gt() %>% 
  tab_header(
    title = md("The ***productivity*** dataset"),
    subtitle = md("Descriptives of five months of data on productivity, consumption, and sleep patterns.")
  ) %>% 
  cols_label(
    n = md("Cases (N)"),
    min = md("Min"),
    max = md("Max"),
    median = md("Median"),
    q1 = md("Q1"),
    q3 = md("Q3"),
    iqr = md("IQR"),
    mad = md("MAD"),
    mean = md("Mean"),
    sd = md("SD"),
    se = md("SE"),
    ci = md("CI")
  ) %>%  
  opt_align_table_header(align = "center") %>% 
  tab_source_note(
    source_note = md("*Data obtained from local matrix.*")
  ) %>% 
  tab_footnote(
    footnote = "Not a standardized unit.",
    locations = cells_stub(rows ="Coffee_Cups")
  )

Problem问题

The issue is that while this script produce the table, I'm unable to get the footnote to work, as it doesn't recognize "Coffee_Cups" as a row.问题是,虽然这个脚本生成表格,但我无法让脚注工作,因为它不能将“Coffee_Cups”识别为一行。 This is what it looks like without the tab_footnote command:这是没有tab_footnote命令的样子:

在此处输入图像描述

This is the error it gives me, which doesn't help much:这是它给我的错误,它没有多大帮助:

Error: The following row(s) do not exist in the data: Coffee_Cups

I've tried tinkering with the other subcommands in tab_footnote but don't get any successful runs.我尝试修改tab_footnote中的其他子命令,但没有成功运行。 What should I do?我应该怎么办?

I comment here because I need more characters.我在这里评论是因为我需要更多角色。

If you run the following:如果您运行以下命令:

> w$`_data`
# A tibble: 8 × 13
  variable         n   min   max median      q1      q3   iqr    mad    mean     sd      se     ci
  <chr>        <dbl> <dbl> <dbl>  <dbl>   <dbl>   <dbl> <dbl>  <dbl>   <dbl>  <dbl>   <dbl>  <dbl>
1 Coffee_Cups      6     0     6    3.5    2.25    4.75   2.5   2.22    3.33   2.16   0.882   2.27
2 End_Work         6  1210  1800 1565   1519.   1675    156.  137.   1559.   202.    82.5   212.  
3 Mins_Sleep       6   300   540  480    480     525     45    44.5   470     88.3   36.1    92.7 
4 Mins_Work        6    15   435  140     78.8   299.   220   152.    190    166.    67.9   174.  
5 Start_Work       6   915  1500 1008.   959.   1304.   345   115.   1129.   253.   103.    266.  
6 Tea_Cups         6     0     4    2      0.5     2      1.5   1.48    1.67   1.51   0.615   1.58
7 Time_Wake        6   500   715  600    600     675     75    74.1   619.    78.8   32.2    82.7 
8 Time_Workout     3   730   730  730    730     730      0     0     730      0      0       0 

There you can clearly see that Coffee_Cups is a value inside a column, not a row.在那里您可以清楚地看到 Coffee_Cups 是列内的值,而不是行。

I tried changing the code like this:我尝试像这样更改代码:

tab_footnote(
    footnote = "Not a standardized unit.",
    locations = cells_stub(rows = c(1))
  )

And I get the following error:我收到以下错误:

Error: Can't use NA as column index with `[` at position 1.
Run `rlang::last_error()` to see where the error occurred.
> rlang::last_error()
<error/tibble_error_na_column_index>
Can't use NA as column index with `[` at position 1.
Backtrace:
 1. (function (x, ...) ...
 2. gt:::print.gt_tbl(x)
 3. gt:::as.tags.gt_tbl(x, ...)
 4. gt:::render_as_html(data = x)
 5. gt:::build_data(data = data, context = "html")
 6. gt:::apply_footnotes_to_output(data = data, context = context)
 8. tibble:::`[.tbl_df`(body, footnotes_data_marks$rownum[i], footnotes_data_marks$colname[i])
Run `rlang::last_trace()` to see the full context.

At this point we know that the row was taken, now we need to figure out the problem with the column not matching...至此我们知道该行被取走了,现在我们需要找出列不匹配的问题......

I could make it work using the following:我可以使用以下方法使其工作:

works %>% 
  get_summary_stats() %>% 
  gt() %>% 
  tab_header(
    title = md("The ***productivity*** dataset"),
    subtitle = md("Descriptives of five months of data on productivity, consumption, and sleep patterns.")
  ) %>% 
  cols_label(
    n = md("Cases (N)"),
    min = md("Min"),
    max = md("Max"),
    median = md("Median"),
    q1 = md("Q1"),
    q3 = md("Q3"),
    iqr = md("IQR"),
    mad = md("MAD"),
    mean = md("Mean"),
    sd = md("SD"),
    se = md("SE"),
    ci = md("CI")
  ) %>%  
  opt_align_table_header(align = "center") %>% 
  tab_source_note(
    source_note = md("*Data obtained from local matrix.*")
  ) %>% 
  tab_footnote(
    footnote = "Not a standardized unit.",
    #locations = cells_stub(rows = everything(vars = "Coffee_Cups"))
    locations = cells_body(1,1)
  )

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM