R语言 Kable column_spec对Shiny应用程序的列格式设置不一致

kr98yfug  于 2023-03-15  发布在  其他
关注(0)|答案(1)|浏览(96)

我正在尝试为一个闪亮的应用程序生成一些表,该应用程序将使用kableExtra的column_spec有条件地将列格式化为红色(如果值高于某个数量)和绿色(如果值低于某个数量)。
试图设置表,以便根据值对“% change”列进行着色,但着色与用于检查它的逻辑语句不一致。某些红色值未通过测试。向此块添加条件格式的更好方法是什么,还是我很简单?

dput(tuition_formatted)
structure(list(new_report_date = c("06mar2023", "06mar2023", 
"06mar2023", "06mar2023", "06mar2023", "06mar2023", "06mar2023", 
"06mar2023", "06mar2023", "06mar2023", "06mar2023"), mau = c("UAA", 
"UAA", "UAA", "UAA", "UAA", "UAA", "UAA", "UAA", "UAA", "UAA", 
"UAA"), fee_type = c("10_lower level", "20_upper level", "30_graduate level", 
"50_come home to ak", "60_wue", "70_non-res tuition", "79_other tuition", 
"80_course fees", "85_student fees", "90_facilities fee", "95_network fee"
), new_total_amt = c(13105458, 5469326, 1520224, -558076, -779464, 
3197334, 383246, 1720647.7, 2558324.47, 467556, 846054.5), old_report_date = c("28feb2022", 
"28feb2022", "28feb2022", "28feb2022", "28feb2022", "28feb2022", 
"28feb2022", "28feb2022", "28feb2022", "28feb2022", "28feb2022"
), old_total_amt = c(12989214, 6148041, 1602145, -749950, -712114, 
2720196, 247422, 1823408.72, 2591916.1, 464700, 847045), `percent_change(tuition, 4, 6)` = c(0.89492712953994, 
-11.0395327552305, -5.11320760605313, -25.5849056603774, 9.4577553593947, 
17.5405742821473, 54.8956842964652, -5.63565474228949, -1.29601533012584, 
0.614590058102001, -0.116935936107291), new_report_date = c("06mar2023", 
"06mar2023", "06mar2023", "06mar2023", "06mar2023", "06mar2023", 
"06mar2023", "06mar2023", "06mar2023", "06mar2023", "06mar2023"
), mau = c("UAF", "UAF", "UAF", "UAF", "UAF", "UAF", "UAF", "UAF", 
"UAF", "UAF", "UAF"), fee_type = c("10_lower level", "20_upper level", 
"30_graduate level", "50_come home to ak", "60_wue", "70_non-res tuition", 
"79_other tuition", "80_course fees", "85_student fees", "90_facilities fee", 
"95_network fee"), new_total_amt = c(8372256, 4031839, 2955876, 
-147160, -505975, 2134952, -366465, 1066801, 2860756.5, 106944, 
665812.5), old_report_date = c("28feb2022", "28feb2022", "28feb2022", 
"28feb2022", "28feb2022", "28feb2022", "28feb2022", "28feb2022", 
"28feb2022", "28feb2022", "28feb2022"), old_total_amt = c(7505955, 
4060161, 2941323, -141500, -412631, 1996946, -415665.5, 977499.21, 
2787797, 105576, 636478), `percent_change(tuition, 4, 6)` = c(11.5415160362672, 
-0.697558545092177, 0.494777350192413, 4, 22.6216643926414, 6.91085287233606, 
-11.8365608885029, 9.13574037568788, 2.61710232129527, 1.29574903387133, 
4.60887886148461), new_report_date = c("06mar2023", "06mar2023", 
"06mar2023", "06mar2023", "06mar2023", "06mar2023", "06mar2023", 
"06mar2023", "06mar2023", "06mar2023", "06mar2023"), mau = c("UAS", 
"UAS", "UAS", "UAS", "UAS", "UAS", "UAS", "UAS", "UAS", "UAS", 
"UAS"), fee_type = c("10_lower level", "20_upper level", "30_graduate level", 
"50_come home to ak", "60_wue", "70_non-res tuition", "79_other tuition", 
"80_course fees", "85_student fees", "90_facilities fee", "95_network fee"
), new_total_amt = c(1901016, 835566, 516345, -56034, -264012, 
662220, -78339, 190041, 466000, 71881, 135751), old_report_date = c("28feb2022", 
"28feb2022", "28feb2022", "28feb2022", "28feb2022", "28feb2022", 
"28feb2022", "28feb2022", "28feb2022", "28feb2022", "28feb2022"
), old_total_amt = c(1774422, 976848, 688236, -77542, -284217, 
694482, -138915, 180208, 491378, 74207, 141206), `percent_change(tuition, 4, 6)` = c(7.13437953316629, 
-14.4630484988453, -24.9755897686259, -27.7372262773723, -7.10900473933649, 
-4.64547677261614, -43.6065219738689, 5.45647252064281, -5.16465938646012, 
-3.13447518428181, -3.86315029106412)), class = "data.frame", row.names = c(NA, 
-11L))

  # tuition table that is close to "beta ready".
  output$tuitionTable <- function() {
    tuition_formatted %>%
      select(-c("mau", "mau.1", "mau.2", "fee_type.1", "fee_type.2")) %>%
      dplyr::mutate_if(is.numeric,~round(.,2)) %>%
      kbl(booktabs = T, 
          col.names = c("Fee Type", "Fall 2023 Total", "Fall 2022 Total", "% change",
                        "Fall 2023 Total", "Fall 2022 Total", "% change",
                        "Fall 2023 Total", "Fall 2022 Total", "% change"),
          caption = "Tuition and Revenue ($)",
          format.args = list(big.mark = ",")) %>%
      add_header_above(c(" ","School 1" = 3, "School 2" = 3, "School 3" = 3)) %>%
      column_spec(c(4,7,10), color = "white", 
                  background = ifelse(tuition_formatted$`percent_change(tuition, 4, 6)` >= .01, "red", "green")) %>%
      kable_styling("striped", full_width = T, position = "left", font_size = 12)
  }

tuticity_formatted基于一个经过处理的tuticity data.frame,我尝试从使用tuticity_formatted改为使用经过预处理的data.frame,这与上面的方法存在相同的不一致问题。
我试着用

column_spec(c(4,7,10), color = "white", 
                  background = ifelse(tuition[,7] >= .01, "red", "green"))

也没有正确设置列的格式

我还尝试显式引用原始data.frame列

column_spec(c(4,7,10), color = "white", 
                  background = ifelse(tuition$`percent_change(tuition, 4, 6)` >= .01, "red", "green"))

它又一次没有被正确处理。
我有一种感觉,问题发生在管道,因为我是管道在一个可移动的对象。

a5g8bdjr

a5g8bdjr1#

dput输出中的名称与调用kable时的名称不匹配,因此我修改了select语句,不过这对您来说不是必需的。
我拆分了column_spec调用,这样一列就不会决定所有列的颜色。当我这样做时,我没有使用名称,我使用了索引,因为我在数据中使用的名称与您使用的名称不一致。
我没有删除您对column_spec的原始调用,我只是将其注解掉,以便您可以看到差异。

tuition_formatted %>% select(3:4, 6:7, 11, 13:14, 18, 20:21) %>% 
  dplyr::mutate_if(is.numeric,~round(.,2)) %>%
  kbl(booktabs = T, 
      col.names = c("Fee Type", "Fall 2023 Total", "Fall 2022 Total", "% change",
                    "Fall 2023 Total", "Fall 2022 Total", "% change",
                    "Fall 2023 Total", "Fall 2022 Total", "% change"),
      caption = "Tuition and Revenue ($)",
      format.args = list(big.mark = ",")) %>%
  add_header_above(c(" ","School 1" = 3, "School 2" = 3, "School 3" = 3)) %>%
  column_spec(4, color = "white", 
              background = ifelse(tuition_formatted[, 7] >= .01, "red", "green")) %>% 
  column_spec(7, color = "white",
              background = ifelse(tuition_formatted[, 14] >= .01, "red", "green")) %>% 
  column_spec(10, color = "white",
              background = ifelse(tuition_formatted[, 21] >= .01, "red", "green")) %>% 
  # column_spec(c(4,7,10), color = "white", 
  # background = ifelse(tuition_formatted$`percent_change(tuition, 4, 6)` >= .01, "red", "green")) %>%
  kable_styling("striped", full_width = T, position = "left", font_size = 12)

相关问题