R语言 缩进自动换行文本GT

xoshrz7s  于 2023-02-27  发布在  其他
关注(0)|答案(1)|浏览(185)

我正在尝试缩进GT表格的存根中的文本。存根中的某些文本很长,并且跨多行。当我对存根应用缩进时,文本的第二行(及后续行)不会缩进。我只想缩进存根的整个换行文本。
下面是一个可重现的示例:

library(dplyr)
library(gt)
library(tidyr)
# Get vectors of 2-letter country codes for
# each region of Oceania
Australasia <- c("AU", "NZ")
Melanesia <- c("NC", "PG", "SB", "VU")
Micronesia <- c("FM", "GU", "KI", "MH", "MP", "NR", "PW")
Polynesia <- c("PF", "WS", "TO", "TV")

# Create a gt table based on a preprocessed `countrypops`
countrypops %>%
  filter(country_code_2 %in% c(
    Australasia, Melanesia, Micronesia, Polynesia)
  ) %>%
  filter(year %in% c(1995, 2005, 2015)) %>%
  mutate(region = case_when(
    country_code_2 %in% Australasia ~ "Australasia",
    country_code_2 %in% Melanesia ~ "Melanesia",
    country_code_2 %in% Micronesia ~ "Micronesia",
    country_code_2 %in% Polynesia ~ "Polynesia",
  )) %>%
  pivot_wider(names_from = year, values_from = population) %>%
  arrange(region, desc(`2015`)) %>%
  select(-starts_with("country_code")) %>%
  gt(
    rowname_col = "country_name",
    groupname_col = "region"
  ) %>% 
  tab_style(
    style = list(
      cell_text(indent = pct(8))
    ),
    locations = cells_stub()
  ) %>% 
  cols_width(country_name ~ 150)

缩进不正确的表格图片:

如何在存根行中一致地应用缩进?

v1l68za4

v1l68za41#

Intend(text-indent)仅适用于第一行,对于多行使用padding-left,例如

countrypops %>%
  filter(country_code_2 %in% c(
    Australasia, Melanesia, Micronesia, Polynesia)
  ) %>%
  filter(year %in% c(1995, 2005, 2015)) %>%
  mutate(region = case_when(
    country_code_2 %in% Australasia ~ "Australasia",
    country_code_2 %in% Melanesia ~ "Melanesia",
    country_code_2 %in% Micronesia ~ "Micronesia",
    country_code_2 %in% Polynesia ~ "Polynesia",
  )) %>%
  pivot_wider(names_from = year, values_from = population) %>%
  arrange(region, desc(`2015`)) %>%
  select(-starts_with("country_code")) %>%
  gt(
    rowname_col = "country_name",
    groupname_col = "region"
  ) %>% 
  tab_style(
    style = "padding-left:30px;",
    locations = cells_stub()
  ) %>%
  cols_width(country_name ~ 150)

相关问题