簡體   English   中英

Kable cell_spec 格式刪除表格的行標簽

[英]Kable cell_spec formatting removes row labels of table

在 R markdown 中,我正在嘗試使用 kable ZEFE90A8E604A7C840E8ZF0 有條件地格式化 html 表。 我有一個看起來像這樣的表 bigT_output在此處輸入圖像描述

我使用 Kable 使用此代碼對其進行格式化

    kable(bigT_output[,1:7], booktabs=TRUE, align="c") %>% 
  pack_rows(
    index = c("Performance Standard" = 1, "Percentile Rank (max 100)" = 3, "Productivity" = 2)
    )%>% 
  kable_styling(bootstrap_options = "hover")

它在 html output 中制作了這張漂亮的表格:在此處輸入圖像描述

理想情況下,我想格式化整個第一行(等級)以根據它是 ME、BE、DE 等來更改背景。但只是為了測試,我試圖只更改一列的背景。 當我使用 cell_spec 時,它會刪除所有行標簽並忽略我的其他 kable 格式選項,如下所示:在此處輸入圖像描述

這是代碼:

    bigT_output %>%
mutate(Merch = cell_spec(Merch, "html", background="green")) %>%
kable(bigT_output[,1:7], booktabs=TRUE, align="c") %>% 
  pack_rows(
    index = c("Performance Standard" = 1, "Percentile Rank (max 100)" = 3, "Productivity" = 2)
    )%>% 
  kable_styling(bootstrap_options = "hover")

如何在不丟失行標簽的情況下同時使用副標題使我的表格漂亮並格式化它的某一行? 我嘗試的每件事都不斷出錯。 感謝您的閱讀。

解決方案是將行名保存在創建表的 function 中。 Then call the function, convert to dataframe, mutate to get the colors (mutate_all(funs()), add in row names ( rownames rownames<- (bigT_row_names)), and then apply kable_styling.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM