一次将formattable格式应用于所有列

一次将formattable格式应用于所有列,r,formattable,R,Formattable,我想将dataframe转换为formattable表,并为每列添加自定义格式。是否有一种方法可以将格式应用于所有列,而无需专门调用它们?下面是一些示例代码,解释了我目前拥有的内容: library(formattable) # create df col1 <- c(0, -2, 4) col2 <- c(-1, 0, 2) col3 <- c(-1, -1, 0) df <- data.frame(col1, col2, col3) # set color

我想将dataframe转换为formattable表,并为每列添加自定义格式。是否有一种方法可以将格式应用于所有列,而无需专门调用它们?下面是一些示例代码,解释了我目前拥有的内容:

library(formattable)    

# create df
col1 <- c(0, -2, 4)
col2 <- c(-1, 0, 2)
col3 <- c(-1, -1, 0)
df <- data.frame(col1, col2, col3)

# set colors
red <- "#ff7f7f"
blue <- "#7f9dff"
white <- "#ffffff"

# add custom format
color_format <- formatter('span', 
                          style = x ~ style(color = ifelse(x > 0, blue, 
                                                           ifelse(x < 0, red, white))))

# create table
formattable(df, 
            list("col1" = color_format,
                 "col2" = color_format,
                 "col3" = color_format))
库(格式化表)
#创建df

col1尝试使用lappy

myForm <- function(x) {
              formatter('span',
                        style = x ~ style(
                          color = ifelse(x > 0, blue,
                                         ifelse(x < 0, red, white))
                        ))
}
formattable(df, lapply(df, myForm))
myForm 0,蓝色,
ifelse(x<0,红色,白色))
))
}
格式表(df,lappy(df,myForm))