简体   繁体   English

R的Huxtable包:使用set_background_color()时遇到问题

[英]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)中的某些单元着色,如下所示: DATAFRAME_with_highlights

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)/相应的单元格中:

HUXTABLE_with_highlights

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.

相关问题 使用 R 包 Huxtable 更改 RTF 输出的方向 - Change orientation of RTF output using R package Huxtable 适用于R的Huxtable包:使用huxreg()在回归表中的两个模型列表之间的颜色编码差异 - Huxtable package for R: Color coding differences between two lists of models in regression table with huxreg() R的Huxtable包:如何为huxtables的caption()和footnote()设置font_size()? - Huxtable package for R: How to set font_size() for caption() and footnote() for huxtables? 如何使用 R 中的“huxtable”库为表格中所需的单元格着色。 哪种方式更优雅? - How to color the required cells in the table using "huxtable" library in R. Which is more elegant way to do it? R 的 Huxtable 包:如何在 bookdown 中正确引用 huxtables? - Huxtable package for R: How to correctly reference huxtables in bookdown? 适用于R的Huxtable包:对齐列无法按针织文档的预期工作 - Huxtable package for R: Align Columns Not Working As Expected in Knitted Doc 使用 by_cases() 时 R 中的 Huxtable 格式错误 - Huxtable in R give wrong formatting when using by_cases() Huxtable package for R:输出到 Word 时如何正确引用 bookdown 中的 huxtables - Huxtable package for R: How to correctly reference huxtables in bookdown when outputting to Word 在R中的predictABEL包中使用plotCalibration()的问题 - Problems with using plotCalibration() from the predictABEL package in R 使用Vim-R时设置tmux / R窗口背景色 - set tmux/R window background color when using Vim-R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM