rstudio / pagedown

Paginate the HTML Output of R Markdown with CSS for Print
https://pagedown.rbind.io
Other
889 stars 129 forks source link

GT table formatting #316

Closed JFern83 closed 1 year ago

JFern83 commented 1 year ago

I am using GT table and am confused by how it is generating the output.

Code is below. Before passing it through gt() the dataframe produced by the "group_by" function formats the table as expected i.e. 4 headings and 12 rows. After gt() it does something odd to to the 'course' and 'unit' variables, pics below. Any ideas or suggestions for formatting the gt table in the same way the dataframe is constructed?

Screenshot 2023-07-08 at 18 12 42 Screenshot 2023-07-08 at 15 45 02 Screenshot 2023-07-08 at 15 44 34

final_table <- df %>% group_by(Unit, Course) %>% summarise_at(.vars = c("RR", "Score"), .funs = ~mean(., na.rm = T)) %>% arrange(desc(Score)) %>% gt() %>% cols_label( RR = ("Response Rate %"), Score = "Mean Score") %>% opt_align_table_header(align = "left") %>% fmt_number(columns = Score) %>% fmt_number(columns = RR, scale_by = 100/1, decimals = F) %>% cols_width( Unit ~ px(150), RR ~px(150), Score ~px(150)) %>% cols_align("center") %>% data_color( columns = Score, colors = scales::col_numeric( palette = c( "red","orange","green"), domain = c(3,3.5,4.0,4.5,5)))

JFern83 commented 1 year ago
final_table <- df %>% 
  group_by(Unit, Course) %>% 
  summarise_at(.vars = c("RR", "Score"), .funs = ~mean(., na.rm = T)) %>% 
  arrange(desc(Score)) %>% 
  gt() %>% 
  cols_label(
    RR = ("Response Rate %"), 
    Score = "Mean Score") %>% 
  opt_align_table_header(align = "left") %>% 
  fmt_number(columns = Score) %>% 
  fmt_number(columns = RR, scale_by = 100/1, decimals = F) %>% 
  cols_width(
    Unit ~ px(150),
    RR ~px(150),
    Score ~px(150)) %>% 
  cols_align("center") %>% 
  data_color(
    columns = Score, 
    colors = scales::col_numeric(
      palette = c(
        "red","orange","green"),
             domain = c(3,3.5,4.0,4.5,5)))
#> Error in df %>% group_by(Unit, Course) %>% summarise_at(.vars = c("RR", : could not find function "%>%"

Created on 2023-07-08 with reprex v2.0.2

cderv commented 1 year ago

Do you see this only with pagedown output ? Or is it the same output in R console when you run the gt code ?

Why open the issue in this repo ? Your example does not even use pagedown.

You have some Q&A forum dedicated to generic question (like https://community.rstudio.com/). Wouldn't it be better to ask there for gt community to help ?

JFern83 commented 1 year ago

Yes apologies, new to Github and think I've posted this issue in the wrong place! The general Q&A forum looks much more appropriate.

If interested, it is in the R console that the problem occurred, but by adding ungroup prior to passing to gt() the problem was resolved.