fmt_number()
的引用说可以使用表达式选择行:
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})'
)
还尝试了引用的等效项。
这个包很新,如果我错过了一些明显的东西,抱歉!
最佳答案
您还必须指定列
:
df %>%
gt() %>%
fmt_number(
columns = where(is.numeric),
rows = birth_year > 20,
pattern = '({x})'
)
这给了我们(作为屏幕截图/示例):
关于r - 使用表达式选择包 `gt` 中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69635119/