I am using the package formattable to format all data.frames of a list of data.frames. For each data.frame, columns are compared with the previous one. Nevertheless as each data.frame contains different periods, the column names are changing. I am looking for a manner to affect the same style to all columns of all data.frames without using column names.
For example, I start with the table below:
library(formattable)
df <- data.frame(2018 = runif(8),
2019 = runif(8),
2020 = runif(8))
tableF <-
formattable(df, list(
`2019`= formatter("span", style = ~ style(color = ifelse(`2019` >`2018`, "green", "red")),
~ icontext(ifelse(`2019` >`2018`,"arrow-up", "arrow-down"), `2019`)),
`2020`= formatter("span", style = ~ style(color = ifelse(`2020` >`2019`, "green", "red")),
~ icontext(ifelse(`2020` >`2019`,"arrow-up", "arrow-down"), `2020`))
))
tableF
But the next table I want to design is the following
df <- data.frame(2018_s1 = runif(8),
2018_s2 = runif(8),
2019_s1 = runif(8),
2019_s2 = runif(8),
2020_s1 = runif(8),
2020_s2 = runif(8))
Is there a way define a generic style and to affect it to multiple columns? Something like:
target = colnames(df)[-1],
comp.target = colnames(df)[-ncol(df)],
style = ~ style(color = ifelse(target > comp.target, "green", "red")),
~ icontext(ifelse(target > comp.target,"arrow-up", "arrow-down"), target ))
Any help would be appreciated.