问题描述
尝试在 flextable 中使用 scales::col_numeric 函数来有条件地格式化表格的一部分。
使用 flextable 自己的文档:
library(flextable)
ft_2 <- flextable(head(iris))
colourer <- col_numeric(
palette = c("wheat","red"),domain = c(0,7))
bg(ft_2,j = c("Sepal.Length","Sepal.Width","Petal.Length","Petal.Width"),bg = colourer)
但如果只应格式化前 2 行会导致错误。
bg(ft_2,i = 1:2,bg = colourer)
x$data[i,j]
在 colorer 中抛出 debug() 显示 x 是整列而不是前 2 行。
非常感谢任何帮助。
解决方法
这可能是 packageVersion('flextable')
的问题,因为它适用于 (0.6.6
) 和 packageVersion('scales')
1.1.1`
library(flextable)
ft_2 <- flextable(head(iris))
colourer <- col_numeric(
palette = c("wheat","red"),domain = c(0,7))
bg(ft_2,i = 1:2,j = c("Sepal.Length","Sepal.Width","Petal.Length","Petal.Width"),bg = colourer)
-输出