首页 > 解决方案 > 在 R 中使用 formattable 组合列

问题描述

我正在尝试使用 formattable on 来更好地可视化表格。我想要做的是将列名称和差异组合在一起显示在一列下。我已经格式化了列差异。我如何在这里组合以将它们显示为一列:

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))
  )) 
)

在此处输入图像描述

标签: rformattable

解决方案


推荐阅读