簡體   English   中英

R的Huxtable包:使用set_background_color()時遇到問題

[英]Huxtable package for R: Problems with using set_background_color()

我嘗試為數據框(df_data)中的某些單元着色,如下所示: DATAFRAME_with_highlights

Huxtable簡介的啟發,我嘗試了以下操作:

library(huxtable)
as_hux(df_table)                                                  %>%
 set_background_color(where(df_table["choice_mean"] < 2), 'red')  %>%
 set_background_color(where(df_table["N"] > 110), 'yellow')   

上面的命令在正確的行中為單元格上色–但僅在第一列而不是所需的列(N,choice_mean)/相應的單元格中:

HUXTABLE_with_highlights

非常感謝您的簡短答復和幫助!

問題是where(DF_table["choice_mean"] < 2)

這是正在發生的事情,其中​​包含一些數據,以便其他人可以重現該問題(提示):

DF_table <- data.frame(choice_mean = c(1,2,3,1,3), N = c(100, 120, 100, 90, 100))
where(DF_table["choice_mean"] < 2)
##     row col
## [1,]   1   1
## [2,]   4   1

您僅將數據幀的一部分傳遞到where ,它正確地告訴您,在數據幀的那(1列)部分中,第1行第1行和第4行第1行小於2。然后得出錯誤的信息給set_background_color

您可以使用標准R子集來解決此問題:

DF_table <- as_hux(DF_table)
set_background_color(DF_table, DF_table$choice_mean < 2, 'choice_mean', 'red')
set_background_color(DF_table, DF_table$N > 110, 'N', 'yellow')

在此, DF_table$N > 110指定行, 'N'列。

這與普通的R子集相同:

DF_table[DF_table$N > 110, 'N']
## 120

暫無
暫無

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

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