简体   繁体   English

跨单行格式化gt表的方法?

[英]Way to format gt table across a single row?

I'm trying to format my gt table with conditions and am at a bit of a standstill.我正在尝试使用条件格式化我的 gt 表,但有点停滞不前。 Most of the examples I'm finding are formatting based off of column headers.我发现的大多数示例都是基于列标题的格式。

Here's the code for an example table with the first column named "row" w/ values 1-6.这是示例表的代码,其中第一列名为“row”,值为 1-6。 How would i get the gt table to add a color ONLY to values > 3.0 across row 1?我如何让 gt 表仅向第 1 行的值 > 3.0 添加颜色? This is my first question, apologies if I messed something up!这是我的第一个问题,如果我搞砸了,请道歉!

这是我希望桌子的样子。

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="_"
  )

We could add tab_style with cell_fill and specify the locations with columns and rows我们可以用cell_fill添加tab_style并用columnsrows指定locations

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

-ouptut -输出

在此处输入图片说明


Update更新

Based on the comments, if we need to make use of a condition for each column, then we could use a for loop to loop over the column names and add the condition layer and update the original gt object ('tbl1')根据评论,如果我们需要为每列使用一个条件,那么我们可以使用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 
       )
     )  
}

-output -输出

在此处输入图片说明

if you are looking to hightlight only those cells with a value greater than 3 - meaning you want not to inform them by name like akrun did - I am afraid you have to go brute force (condition for each column).如果您只想突出显示那些值大于 3 的单元格 - 这意味着您不想像 akrun 那样通过名称通知它们 - 恐怕您必须使用蛮力(每列的条件)。 I worked for the first 5 columns as it is just repetition:我为前 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="_"
  )

在此处输入图片说明

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

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