使用 flextable r 包格式化多列
Formatting multiple columns with flextable r package
是否可以使用 flexitable 一行脚本格式化多列?
该示例包括两种不同类型的双精度变量。我想:
- 将大于 1000 的变量四舍五入到最接近的 1000 并添加千位逗号格式,并且
- 小数点后一位到两位的变量取整
这使用 flextable 相对简单,但对于更大的数据集来说就很麻烦了。我看不到更有效地执行此操作的方法。
我可以使用 dplyr 预处理数据(尽管逗号格式需要将变量类型从双精度更改为字符)。
如果可能的话,我更愿意在 flextable 中执行此操作。
我知道 Huxtable 可以进行多列格式化。
用户是否可以创建定制的 set_formatter_type 函数?
MWE
set.seed(100)
tib <- tibble(a = letters[1:4],
b = runif(4)*sample(c(10000, 1000000, 10000000), 4, replace = TRUE),
c = runif(4)*sample(c(10000, 1000000, 10000000), 4, replace = TRUE),
d = runif(4)*sample(c(10000, 1000000, 10000000), 4, replace = TRUE),
e = runif(4),
f = runif(4))
regulartable(tib) %>%
set_formatter(b = function(x) format(round(x, -3), big.mark = ",")) %>%
set_formatter(c = function(x) format(round(x, -3), big.mark = ",")) %>%
set_formatter(d = function(x) format(round(x, -3), big.mark = ",")) %>%
set_formatter(e = function(x) round(x, 2)) %>%
set_formatter(f = function(x) round(x, 2)) %>%
print(preview = "docx")
是的,可以在此处找到文档:https://davidgohel.github.io/flextable/articles/format.html#set_formatter-function
这里:
https://davidgohel.github.io/flextable/reference/set_formatter.html
set_formatter(b = function(x) format(round(x, -3), big.mark = ","),
c = function(x) format(round(x, -3), big.mark = ","),
d = function(x) format(round(x, -3), big.mark = ","),
e = function(x) round(x, 2),
f = function(x) round(x, 2))
是否可以使用 flexitable 一行脚本格式化多列?
该示例包括两种不同类型的双精度变量。我想:
- 将大于 1000 的变量四舍五入到最接近的 1000 并添加千位逗号格式,并且
- 小数点后一位到两位的变量取整
这使用 flextable 相对简单,但对于更大的数据集来说就很麻烦了。我看不到更有效地执行此操作的方法。
我可以使用 dplyr 预处理数据(尽管逗号格式需要将变量类型从双精度更改为字符)。 如果可能的话,我更愿意在 flextable 中执行此操作。 我知道 Huxtable 可以进行多列格式化。 用户是否可以创建定制的 set_formatter_type 函数?
MWE
set.seed(100)
tib <- tibble(a = letters[1:4],
b = runif(4)*sample(c(10000, 1000000, 10000000), 4, replace = TRUE),
c = runif(4)*sample(c(10000, 1000000, 10000000), 4, replace = TRUE),
d = runif(4)*sample(c(10000, 1000000, 10000000), 4, replace = TRUE),
e = runif(4),
f = runif(4))
regulartable(tib) %>%
set_formatter(b = function(x) format(round(x, -3), big.mark = ",")) %>%
set_formatter(c = function(x) format(round(x, -3), big.mark = ",")) %>%
set_formatter(d = function(x) format(round(x, -3), big.mark = ",")) %>%
set_formatter(e = function(x) round(x, 2)) %>%
set_formatter(f = function(x) round(x, 2)) %>%
print(preview = "docx")
是的,可以在此处找到文档:https://davidgohel.github.io/flextable/articles/format.html#set_formatter-function
这里: https://davidgohel.github.io/flextable/reference/set_formatter.html
set_formatter(b = function(x) format(round(x, -3), big.mark = ","),
c = function(x) format(round(x, -3), big.mark = ","),
d = function(x) format(round(x, -3), big.mark = ","),
e = function(x) round(x, 2),
f = function(x) round(x, 2))