【问题标题】:Way to format gt table across a single row?跨单行格式化gt表的方法?
【发布时间】:2021-07-15 18:57:57
【问题描述】:

我正在尝试使用条件格式化我的 gt 表,但有点停滞不前。我发现的大多数示例都是基于列标题的格式。

这是一个示例表的代码,其中第一列名为“行”,值为 1-6。我如何让 gt 表只为第 1 行的 > 3.0 的值添加颜色?这是我的第一个问题,如果我搞砸了,请道歉!

library(tidyverse)  
library(gt) 

iris %>%   
  group_by(Species) %>%   
  slice_max(Sepal.Length, n=5) %>%   
  group_by(Species) %>% 
  mutate(row=row_number()) %>% 
  pivot_longer(-c(Species, row)) %>%
  mutate(Species = str_to_title(Species),
         name = gsub("\\.", " ", name)) %>% 
  pivot_wider(names_from=c(Species, name), values_from=value)%>% 
  gt() %>% 
  tab_spanner_delim(
    delim="_"
  )

【问题讨论】:

    标签: r dplyr tidyverse gt


    【解决方案1】:

    我们可以添加tab_stylecell_fill,并指定locationscolumnsrows

    library(dplyr)
    library(tidyr)
    library(gt) 
    library(stringr)
    
    iris %>%   
      group_by(Species) %>%   
      slice_max(Sepal.Length, n=5) %>%   
      group_by(Species) %>% 
      mutate(row=row_number()) %>% 
      pivot_longer(-c(Species, row)) %>%
      mutate(Species = str_to_title(Species),
             name = gsub("\\.", " ", name)) %>% 
      pivot_wider(names_from=c(Species, name), values_from=value)%>% 
      gt() %>%
      tab_spanner_delim(
        delim="_"
      ) %>%
      
      tab_style(
        style = list(
          cell_fill(color = "lightgreen")
          
        ),
        locations = cells_body(
          columns = c(`Setosa_Sepal Length`, `Setosa_Sepal Width`,
      `Versicolor_Sepal Length`, `Versicolor_Sepal Width`, `Versicolor_Petal Length`,
        `Virginica_Sepal Length`,`Virginica_Sepal Width`, `Virginica_Petal Length`),
                    
          rows = 1
        )
      )
    

    -输出


    更新

    基于 cmets,如果我们需要为每一列使用条件,那么我们可以使用 for 循环来循环列名并添加条件层并更新原始 gt 对象( 'tbl1')

    tbl1 <- iris %>%   
      group_by(Species) %>%   
      slice_max(Sepal.Length, n=5) %>%   
      group_by(Species) %>% 
      mutate(row=row_number()) %>% 
      pivot_longer(-c(Species, row)) %>%
      mutate(Species = str_to_title(Species),
             name = gsub("\\.", " ", name)) %>% 
      pivot_wider(names_from=c(Species, name), values_from=value)%>% 
      gt() %>%
      tab_spanner_delim(
        delim="_"
      ) 
    
    nm1 <- names(tbl1$`_data`)[-1]
    
    for(i in seq_along(nm1)) {
      
       tbl1 <- tbl1 %>%
         tab_style(
           style = list(
             cell_fill(color = "lightgreen")
             
           ),
           locations = cells_body(
             columns = nm1[i],
             
            rows = seq_along(tbl1$`_data`[[nm1[i]]]) == 1 &  
                  tbl1$`_data`[[nm1[i]]] > 3 
           )
         )  
    }
    

    -输出

    【讨论】:

    • 据我了解,OP 只想突出显示第一行中值大于 3 的单元格,而不是专门通知它们 - 我可能错了
    • @DPH 抱歉,之前没有阅读该部分。更新帖子
    • 非常感谢这两个答案!尝试两种方式对我很有帮助。
    【解决方案2】:

    如果您只想突出显示值大于 3 的单元格 - 这意味着您不想像 akrun 那样按名称通知它们 - 恐怕您必须使用蛮力(每列的条件)。我为前 5 列工作,因为它只是重复:

    library(tidyverse)
    library(gt) 
    
    df <- iris %>%   
      group_by(Species) %>%   
      slice_max(Sepal.Length, n=5) %>%   
      group_by(Species) %>% 
      mutate(row=row_number()) %>% 
      pivot_longer(-c(Species, row)) %>%
      mutate(Species = str_to_title(Species),
             name = gsub("\\.", " ", name)) %>% 
      pivot_wider(names_from=c(Species, name), values_from=value) 
    
    df%>% 
      gt() %>% 
     tab_style(
        style = list(
          cell_fill(color = "lightgreen")
          ),
        locations = cells_body(
                     columns =  vars(`Setosa_Sepal Length`),
                     rows = row == 1 & `Setosa_Sepal Length` > 3
        )) %>%
     tab_style(
        style = list(
          cell_fill(color = "lightgreen")
          ),
        locations = cells_body(
                     columns =  vars(`Setosa_Sepal Width`),
                     rows = row == 1 & `Setosa_Sepal Width` > 3
        )) %>%
     tab_style(
        style = list(
          cell_fill(color = "lightgreen")
          ),
        locations = cells_body(
                     columns =  vars(`Setosa_Petal Length`),
                     rows = row == 1 & `Setosa_Petal Length` > 3
        )) %>%
     tab_style(
        style = list(
          cell_fill(color = "lightgreen")
          ),
        locations = cells_body(
                     columns =  vars(`Setosa_Petal Width`),
                     rows = row == 1 & `Setosa_Petal Width` > 3
        )) %>%
     tab_style(
        style = list(
          cell_fill(color = "lightgreen")
          ),
        locations = cells_body(
                     columns =  vars(`Versicolor_Sepal Length`),
                     rows = row == 1 & `Versicolor_Sepal Length` > 3
        )) %>% 
      tab_spanner_delim(
        delim="_"
      )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-15
      • 2012-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多