r-通过FlexTables列表应用条件文本修改



我有一个包含32个flextables的列表,其中包含5个不同的列。我想根据一个条件将color和bold函数应用于两个单独的列。

我的方法如下:

###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函数将该函数应用于flextables列表x,基于条件值y:

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)

最新更新