在数据表R Shiny上使用多种条件和格式 [英] Use multiple conditions and formats on datatable R shiny
问题描述
条件格式数据表R Shiny(多种条件和格式)
Conditional Formatting datatable R shiny (multiple conditions and formats)
在此处回答如何突出显示最大行数的问题:
DT数据表R Shiny中的条件格式
The question how to highlight the max on each row is answered here: Conditional formatting in DT Data Table R Shiny
我不仅要用绿色突出显示每列的最大值,用红色突出显示最小值,还要使那些单元格>该列的中位数以粗体显示。我该如何工作?
I want to highlight not only the maximum of each column in green, and the minimum in red, but also make those cells > median of that column appear in bold. How do I make that work?
下面的代码来自链接的问题,适合我的需要。但不幸的是没有用。
The below code is from the linked question, adapted for my needs. But unfortunately not working.
library(shinydashboard)
library(DT)
library(magrittr)
entity <- c('entity1', 'entity2', 'entity3')
value1 <- c(21000, 23400, 26800)
value2 <- c(21234, 23445, 26834)
value3 <- c(21123, 234789, 26811)
value4 <- c(27000, 23400, 26811)
entity.data <- data.frame(entity, value1, value2, value3, value4)
# Create a vector of max values
max_val <- apply(entity.data[, -1], 2, max)
# Create a vector of min values
min_val <- apply(entity.data[, -1], 2, min)
# Create a vector of median values
median_val <- apply(entity.data[, -1], 2, median)
header <- dashboardHeader()
sidebar <- dashboardSidebar()
body <- dashboardBody(DT::dataTableOutput("entity.dataTable"))
shinyApp(
ui = dashboardPage(header, sidebar, body),
server = function(input, output) {
output$entity.dataTable <- renderDataTable({
DT::datatable(
entity.data,
rownames = FALSE,
) %>%
formatStyle(columns = 2:5,
backgroundColor = styleEqual(levels = max_val, values = rep("green", length(max_val))))%>%
formatStyle(columns = 2:5,
backgroundColor = styleEqual(levels = min_val, values = rep("red", length(min_val))))
})
}
)
推荐答案
由于每个列都有单独的值,因此必须为每个列分别分配值
since you have separate values for each column you have to asign the values separately for each column
shinyApp(
ui = dashboardPage(header, sidebar, body),
server = function(input, output) {
output$entity.dataTable <- renderDataTable({
DT::datatable(
entity.data,
rownames = FALSE,
) %>%
formatStyle(columns = 2,
backgroundColor = styleInterval(cuts =c(min_val[1],max_val[1]-1), values = c("#F00","none","#00F")),
fontWeight = styleInterval(cuts= median_val[1]-1,values = c("normal","bold"))) %>%
formatStyle(columns = 3,
backgroundColor = styleInterval(cuts =c(min_val[2],max_val[2]-1), values = c("#F00","none","#00F")),
fontWeight = styleInterval(cuts= median_val[2]-1,values = c("normal","bold")))%>%
formatStyle(columns = 4,
backgroundColor = styleInterval(cuts =c(min_val[3],max_val[3]-1), values = c("#F00","none","#00F")),
fontWeight = styleInterval(cuts= median_val[3]-1,values = c("normal","bold")))%>%
formatStyle(columns = 5,
backgroundColor = styleInterval(cuts =c(min_val[4],max_val[4]-1), values = c("#F00","none","#00F")),
fontWeight = styleInterval(cuts= median_val[4]-1,values = c("normal","bold")))
})
}
)
希望这会有所帮助!
这篇关于在数据表R Shiny上使用多种条件和格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!