简体   繁体   English

R中带有Formattable包的行条件

[英]Conditionals by row with Formattable package in R

I want to know how to assign a conditional format to a table so that each row has a different condition that needs to be fulfilled.我想知道如何为表分配条件格式,以便每一行都有不同的需要满足的条件。 So that in statistic row, if the value <0.96 it turns red, and in the case of p-value row if the value is > 0.05 it turns red.因此,在统计行中,如果值 <0.96 则变为红色,而在 p 值行的情况下,如果值 > 0.05 则变为红色。

I'm formatting from the following dataframe我正在从以下数据帧进行格式化

df_try
    Normal        Jan        Feb
1  stistic 0.93069466 0.90404849
2 p-values 0.05123532 0.01056474

My condition formatting is in the code imporvement_formatter but naturally, it turns all values red because all are <0.96.我的条件格式在代码 imporvement_formatter 中,但很自然地,它会将所有值变为红色,因为所有值都 <0.96。 I need a second condition for the next row that works for the >0.05 condition我需要适用于 >0.05 条件的下一行的第二个条件

i2 <- df_try %>%
+   select(c(`Normal`,`Jan`, `Feb`))
> formattable(i2)
improvement_formatter <- 
+   formatter("span", 
+             style = x ~ style(
+               font.weight = "bold", 
+               color = ifelse(x < 0.96, customRed, "black")))
> 
> formattable(i2, align =c("l","c","c"), list(
+   `Indicator Name` = 
+     formatter("span", style = ~ style(color = "grey",font.weight = "bold")), 
+   `Jan` = improvement_formatter
+ ))

Have a look at the area function.看看area函数。 It helps to use formatters on rows and not just on columns.它有助于在行上使用格式化程序,而不仅仅是在列上。 So you can define your formatters like you did and then use them specificly on certain rows and columns.因此,您可以像以前一样定义格式化程序,然后专门在某些行和列上使用它们。 Here is an example:下面是一个例子:

library(formattable)

df_try <- structure(list(Normal = structure(2:1, .Label = c("p-values", 
                                                            "stistic"), class = "factor"), Jan = c(0.93069466, 0.05123532
                                                            ), Feb = c(0.90404849, 0.01056474)), class = "data.frame", row.names = c("1", 
                                                                                                                                     "2"))

first_col_formatter <- formatter("span", 
                                 style = ~ style(color = "grey",
                                                 font.weight = "bold"))

improvement_formatter <- formatter("span", 
                style = x ~ style(
                font.weight = "bold", 
                color = ifelse(x < 0.96, "red", "black")))

improvement2_formatter <- formatter("span", 
                                   style = x ~ style(
                                     font.weight = "bold", 
                                     color = ifelse(x > 0.05, "red", "black")))

formattable(df_try,
            align =c("l","c","c"),
            list(Normal = first_col_formatter,
              area(row = 1, col = -1) ~ improvement_formatter,
                 area(row = 2, col = -1) ~ improvement2_formatter))

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM