[英]Huxtable package for R: Problems with using set_background_color()
I tried to colorize certain cells in a data frame (df_data) as highlighted below: 我尝试为数据框(df_data)中的某些单元着色,如下所示:
Inspired by the Introduction to Huxtable I tried the following: 受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')
The above commands colored the cells in the correct row – but only in the first column instead of the desired columns (N, choice_mean)/ the respective cells: 上面的命令在正确的行中为单元格上色–但仅在第一列而不是所需的列(N,choice_mean)/相应的单元格中:
Many thanks for a brief reply and help! 非常感谢您的简短答复和帮助!
The problem is the where(DF_table["choice_mean"] < 2)
. 问题是where(DF_table["choice_mean"] < 2)
。
Here's what's going on, with some data so others can reproduce the problem (hint hint): 这是正在发生的事情,其中包含一些数据,以便其他人可以重现该问题(提示):
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
You've only passed in part of the data frame to where
, and it correctly tells you that in that (1-column) part of the data frame, row 1 col 1 and row 4 col 1 are less than 2. This then gives the wrong info to set_background_color
. 您仅将数据帧的一部分传递到where
,它正确地告诉您,在数据帧的那(1列)部分中,第1行第1行和第4行第1行小于2。然后得出错误的信息给set_background_color
。
You can get round this by using standard R subsetting: 您可以使用标准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')
Here, DF_table$N > 110
specifies the rows, and 'N'
the column. 在此, DF_table$N > 110
指定行, 'N'
列。
This is just the same as normal R subsetting: 这与普通的R子集相同:
DF_table[DF_table$N > 110, 'N']
## 120
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.