1

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. 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. This is what it looks like without the tab_footnote command:

enter image description here

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. What should I do?

Shawn Hemelstrand
  • 2,676
  • 4
  • 17
  • 30

1 Answers1

1

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.

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)
  )

enter image description here

AugtPelle
  • 549
  • 1
  • 10