通过 FlexTable 列表应用条件文本修改
Apply Conditional Text Modifications Through a List of FlexTables
我有一个包含 5 个不同列的 32 个 flextables 的列表。我想根据条件将颜色和粗体功能应用于 2 个单独的列。
我的做法如下:
###Create a function that takes an input x (the flextable) and an input y (the values that will be conditioned on
f <- function(x,y){
x <- color( x,i = y <= 0.05, j=4,color="red")
x <- bold( x, i = y <= .05, j=4)
x
我按如下方式创建 y 值。 tablerep 包含 32 个数据帧的列表,我要作为条件的列是第 4 列:
highlight <- lapply(tablerep,function(i) sprintf('%.3f',as.numeric(as.character(unlist(i[4])))))
然后我使用 mapply 函数根据条件值 y 将该函数应用于 flextables 列表 x:
MyFT <- mapply(f,MyFT,highlight)
出于某种原因,我的输出如下所示:
> MyFT
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
header List,8 List,8 List,8 List,8 List,8 List,8 List,8 List,8 List,8
body List,8 List,8 List,8 List,8 List,8 List,8 List,8 List,8 List,8
footer List,8 List,8 List,8 List,8 List,8 List,8 List,8 List,8 List,8
col_keys Character,5 Character,5 Character,5 Character,5 Character,5 Character,5 Character,5 Character,5 Character,5
caption List,2 List,2 List,2 List,2 List,2 List,2 List,2 List,2 List,2
blanks Character,0 Character,0 Character,0 Character,0 Character,0 Character,0 Character,0 Character,0 Character,0
properties List,2 List,2 List,2 List,2 List,2 List,2 List,2 List,2 List,2
[,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18]
而不是 flextables 列表。任何人都可以提供有关为什么会这样的见解吗?
这是关于 mapply
将结果缩减为矩阵,因为参数 SIMPLIFY
的默认值为 TRUE。要停止它并获取列表,请使用:
MyFT <- mapply(f,MyFT,highlight, SIMPLIFY = FALSE)
我有一个包含 5 个不同列的 32 个 flextables 的列表。我想根据条件将颜色和粗体功能应用于 2 个单独的列。
我的做法如下:
###Create a function that takes an input x (the flextable) and an input y (the values that will be conditioned on
f <- function(x,y){
x <- color( x,i = y <= 0.05, j=4,color="red")
x <- bold( x, i = y <= .05, j=4)
x
我按如下方式创建 y 值。 tablerep 包含 32 个数据帧的列表,我要作为条件的列是第 4 列:
highlight <- lapply(tablerep,function(i) sprintf('%.3f',as.numeric(as.character(unlist(i[4])))))
然后我使用 mapply 函数根据条件值 y 将该函数应用于 flextables 列表 x:
MyFT <- mapply(f,MyFT,highlight)
出于某种原因,我的输出如下所示:
> MyFT
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
header List,8 List,8 List,8 List,8 List,8 List,8 List,8 List,8 List,8
body List,8 List,8 List,8 List,8 List,8 List,8 List,8 List,8 List,8
footer List,8 List,8 List,8 List,8 List,8 List,8 List,8 List,8 List,8
col_keys Character,5 Character,5 Character,5 Character,5 Character,5 Character,5 Character,5 Character,5 Character,5
caption List,2 List,2 List,2 List,2 List,2 List,2 List,2 List,2 List,2
blanks Character,0 Character,0 Character,0 Character,0 Character,0 Character,0 Character,0 Character,0 Character,0
properties List,2 List,2 List,2 List,2 List,2 List,2 List,2 List,2 List,2
[,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18]
而不是 flextables 列表。任何人都可以提供有关为什么会这样的见解吗?
这是关于 mapply
将结果缩减为矩阵,因为参数 SIMPLIFY
的默认值为 TRUE。要停止它并获取列表,请使用:
MyFT <- mapply(f,MyFT,highlight, SIMPLIFY = FALSE)