可格式化的 R 包:根据数字阈值有条件地格式化单元格的内容

Formattable R package: conditionaly formatting the cells' content on the basis of a numeric threshold

我有如下数据框

mydata <- structure(list(Wife = c(15.972, 12.715, 8.333, 6.276, 2.179, 
-1.408, -1.649, -4.647, -7.039, -5.299, -7.411, -9.776, -9.612
), Alternating = c(-2.622, -0.548, -1.331, 3.9, -1.802, 2.08, 
1.481, 9.53, 7.709, -0.953, -4.823, -4.878, -5.245), Husband = c(-7.012, 
-5.823, -3.99, -3.324, -5.828, -4.869, -3.941, 0.515, 9.551, 
-0.868, 4.843, 24.544, -5.812), Jointly = c(-8.283, -7.569, -4.048, 
-6.564, 4.418, 4.284, 4.157, -3.006, -7.306, 7.066, 7.086, -8.306, 
19.397)), row.names = c("Laundry", "Main_meal", "Dinner", "Breakfeast", 
"Tidying", "Dishes", "Shopping", "Official", "Driving", "Finances", 
"Insurance", "Repairs", "Holidays"), class = "data.frame")

其中列出了卡方调整后的标准化残差。使用 'formattable' R 包,我设法得到下面的 table(比 R 控制台中的格式更漂亮):

问题

我找不到根据残差大小有条件地格式化某些单元格的可行选项。我所追求的是(例如)将残差大于 +1.96 的单元格设置为绿色,将残差小于 -1.96 的单元格设置为红色。 我确实查阅了包裹的vignette,没有用。

library(formattable)
library(dplyr)

mydata <- structure(list(Wife = c(
  15.972, 12.715, 8.333, 6.276, 2.179,
  -1.408, -1.649, -4.647, -7.039, -5.299, -7.411, -9.776, -9.612
), Alternating = c(
  -2.622, -0.548, -1.331, 3.9, -1.802, 2.08,
  1.481, 9.53, 7.709, -0.953, -4.823, -4.878, -5.245
), Husband = c(
  -7.012,
  -5.823, -3.99, -3.324, -5.828, -4.869, -3.941, 0.515, 9.551,
  -0.868, 4.843, 24.544, -5.812
), Jointly = c(
  -8.283, -7.569, -4.048,
  -6.564, 4.418, 4.284, 4.157, -3.006, -7.306, 7.066, 7.086, -8.306,
  19.397
)), row.names = c(
  "Laundry", "Main_meal", "Dinner", "Breakfeast",
  "Tidying", "Dishes", "Shopping", "Official", "Driving", "Finances",
  "Insurance", "Repairs", "Holidays"
), class = "data.frame")



style_ci <- function(x) {
  case_when(x > 1.96 ~ "green", x < -1.96 ~ "red", TRUE ~ "black")
}

formattable(mydata, list(
  Wife = formatter("span", style = ~ style(color = style_ci(Wife))),
  Alternating = formatter("span", style = ~ style(color = style_ci(Alternating))),
  Husband = formatter("span", style = ~ style(color = style_ci(Husband))),
  Jointly = formatter("span", style = ~ style(color = style_ci(Jointly)))
))