I am trying to use formattable on for better visualization of the table. What I want to do is combine columns name and difference to be shown together under one column. I have formatted the column difference. How do I combine here to show them as one column:
library(readr)
library(formattable)
df <- data.frame(
id = 1:10,
name = c("Bob", "Ashley", "James", "David", "Jenny",
"Hans", "Leo", "John", "Emily", "Lee"),
test1_score = c(8.9, 9.5, 9.6, 8.9, 9.1, 9.3, 9.3, 9.9, 8.5, 8.6),
test2_score = c(9.1, 9.1, 9.2, 9.1, 8.9, 8.5, 9.2, 9.3, 9.1, 8.8),
stringsAsFactors = FALSE)
formattable(
df %>%
mutate("difference" = df$test1_score - df$test2_score),
list(difference = formatter(
"span",
style = x ~ style(color = ifelse(x <=
0, "green", "red")),
x ~ icontext(ifelse(x <= 0, "arrow-up", "arrow-down"), round(x, 2))
))
)