使用表达式选择包 `gt` 中的行
Selecting rows in package `gt` using expressions
fmt_number()
的参考说明可以 select 行使用表达式:
rows
Optional rows to format. Providing either everything()
(the default) or TRUE
results in all rows in columns being formatted. Can either be a vector of row captions provided in c()
, a vector of row indices, or a helper function focused on selections. The select helper functions are: starts_with()
, ends_with()
, contains()
, matches()
, one_of()
, num_range()
, and everything()
. We can also use expressions to filter down to the rows we need (e.g., [colname_1] > 100 & [colname_2] < 50
).
但是,我还没有找到如何进行这项工作。我尝试了以下方法(这只是一个例子;这些数字转换在此数据集中没有意义):
library(gt)
library(tidyverse)
df = starwars
gt_tb = gt(df) %>%
fmt_number(
rows = [birth_year] > 20,
pattern = '({x})'
)
gt_tb = gt(df) %>%
fmt_number(
columns = mass,
rows = [birth_year] > 20,
pattern = '({x})'
)
gt_tb = gt(df) %>%
fmt_number(
columns = mass,
rows = expression([birth_year] > 20),
pattern = '({x})'
)
gt_tb = gt(df) %>%
fmt_number(
columns = mass,
rows = df[birth_year] > 20,
pattern = '({x})'
)
gt_tb = gt(df) %>%
fmt_number(
columns = mass,
rows = everything([birth_year] > 20),
pattern = '({x})'
)
还尝试了他们引用的等效项。
这个包很新,如果我遗漏了一些明显的东西,抱歉!
您还必须指定 columns
:
df %>%
gt() %>%
fmt_number(
columns = where(is.numeric),
rows = birth_year > 20,
pattern = '({x})'
)
这给了我们(作为 screenshot/sample):
We can also use expressions to filter down to the rows we need (e.g., [colname_1] > 100 & [colname_2] < 50).
该示例使用方括号 ([]
) 来告诉您这些是占位符,这不是有效的语法。
你的情况是
fmt_number(
columns = mass,
rows = birth_year > 20,
pattern = '({x})'
)
fmt_number()
的参考说明可以 select 行使用表达式:
rows
Optional rows to format. Providing either
everything()
(the default) orTRUE
results in all rows in columns being formatted. Can either be a vector of row captions provided inc()
, a vector of row indices, or a helper function focused on selections. The select helper functions are:starts_with()
,ends_with()
,contains()
,matches()
,one_of()
,num_range()
, andeverything()
. We can also use expressions to filter down to the rows we need (e.g.,[colname_1] > 100 & [colname_2] < 50
).
但是,我还没有找到如何进行这项工作。我尝试了以下方法(这只是一个例子;这些数字转换在此数据集中没有意义):
library(gt)
library(tidyverse)
df = starwars
gt_tb = gt(df) %>%
fmt_number(
rows = [birth_year] > 20,
pattern = '({x})'
)
gt_tb = gt(df) %>%
fmt_number(
columns = mass,
rows = [birth_year] > 20,
pattern = '({x})'
)
gt_tb = gt(df) %>%
fmt_number(
columns = mass,
rows = expression([birth_year] > 20),
pattern = '({x})'
)
gt_tb = gt(df) %>%
fmt_number(
columns = mass,
rows = df[birth_year] > 20,
pattern = '({x})'
)
gt_tb = gt(df) %>%
fmt_number(
columns = mass,
rows = everything([birth_year] > 20),
pattern = '({x})'
)
还尝试了他们引用的等效项。
这个包很新,如果我遗漏了一些明显的东西,抱歉!
您还必须指定 columns
:
df %>%
gt() %>%
fmt_number(
columns = where(is.numeric),
rows = birth_year > 20,
pattern = '({x})'
)
这给了我们(作为 screenshot/sample):
We can also use expressions to filter down to the rows we need (e.g., [colname_1] > 100 & [colname_2] < 50).
该示例使用方括号 ([]
) 来告诉您这些是占位符,这不是有效的语法。
你的情况是
fmt_number(
columns = mass,
rows = birth_year > 20,
pattern = '({x})'
)