0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Tidyverseのgt packageで特定文字列に色をつける。

Posted at

目的

Tidyverseのgt packageで下記のように各列の特定文字列に色をつけること。
左の表を右の表にように変更する。

image.pngimage.png

左の表の作成方法


library(tidyverse)
library(gt)

AA <- c("aaa","ab","ac")
BB <- c("ba","bbb","bc")
CC <- c("ca","cb","ccc")

data <- tibble(AA,BB,CC)

data |> gt() 

右の表の作成方法

%in%を使用しても、カラムがたくさんあるとほぼ同じ内容を繰り返す必要があるので、もっと簡単にしたい。


marks <- c("aaa","bbb","ccc")

data |>
  gt() |> 
  gt::tab_style(style = cell_text(color = "red", weight = "bold"),
                locations = list(
                  gt::cells_body(columns = AA,
                                 rows = AA %in% marks ),
                  gt::cells_body(columns = BB,
                                 rows = BB %in% marks ),
                  gt::cells_body(columns = CC,
                                 rows = CC %in% marks )
                  )
  )

自作関数を使った右の表の作成方法

自作関数を使わないで行う方法が見つけられなかったので、自作関数を使用したが、tidyevalの解決に時間がかかった。!!symを勉強しました。


list_of_location <- function(columns, marks){
 
  location = list()
  
  for (i in 1:length(columns)) {
        location[[i]] <- gt::cells_body(columns = !!rlang::sym(columns[i]),
                                        rows = !!rlang::sym(columns[i]) %in% !!marks)
  }
  
  return(location)
}

columns_labels <- c("AA","BB","CC")
marks <- c("aaa","bbb","ccc")

data |> gt() |> 
  gt::tab_style(style = cell_text(color = "red", weight = "bold"),
                locations = list_of_location(columns_labels, marks))

参考

https://excel2rlang.com/rlang-tidyeval-abstract/
https://excel2rlang.com/rlang-tidyeval-usecases/

sessionInfo()

R version 4.4.0 (2024-04-24 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 22000)

Matrix products: default


locale:
[1] LC_COLLATE=Japanese_Japan.utf8  LC_CTYPE=Japanese_Japan.utf8    LC_MONETARY=Japanese_Japan.utf8 LC_NUMERIC=C                    LC_TIME=Japanese_Japan.utf8    

time zone: Asia/Tokyo
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] rlang_1.1.4     gt_0.10.1       lubridate_1.9.3 forcats_1.0.0   stringr_1.5.1   dplyr_1.1.4     purrr_1.0.2     readr_2.1.5     tidyr_1.3.1     tibble_3.2.1    ggplot2_3.5.1  
[12] tidyverse_2.0.0

loaded via a namespace (and not attached):
 [1] gtable_0.3.5      compiler_4.4.0    tidyselect_1.2.1  xml2_1.3.6        scales_1.3.0      fastmap_1.2.0     R6_2.5.1          generics_0.1.3    munsell_0.5.1    
[10] pillar_1.9.0      tzdb_0.4.0        utf8_1.2.4        stringi_1.8.4     sass_0.4.9        timechange_0.3.0  cli_3.6.2         withr_3.0.0       magrittr_2.0.3   
[19] digest_0.6.35     grid_4.4.0        rstudioapi_0.16.0 hms_1.1.3         lifecycle_1.0.4   vctrs_0.6.5       glue_1.7.0        fansi_1.0.6       colorspace_2.1-0 
[28] tools_4.4.0       pkgconfig_2.0.3   htmltools_0.5.8.1

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?