gpt4 book ai didi

r - DT 数据表 R Shiny 中的条件格式

转载 作者:行者123 更新时间:2023-12-04 10:30:50 25 4
gpt4 key购买 nike

我有一个表,其中 5 列和第一列作为字符,其他四列作为数字。我正在使用 DT 数据表在 Shiny App 中显示相同的内容。现在,我需要比较每行的四个列中的每一个和 颜色编码具有最大值的行单元格 .寻找方法来做同样的事情。也看过这个链接 StylingCells但这里所有的列都是数字。

代码

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)

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,
selection = "single",
filter = 'bottom',

extensions = c('Buttons', 'ColReorder', 'FixedHeader', 'Scroller'),
rownames = FALSE,
options = list(
dom = 'Bfrtip',
searching = T,
pageLength = 25,
searchHighlight = TRUE,
colReorder = TRUE,
fixedHeader = TRUE,
filter = 'top',
buttons = c('copy', 'csv', 'excel', 'print'),
paging = TRUE,
deferRender = TRUE,
scroller = TRUE,
scrollX = TRUE,
scrollY = 550

)

)

})
}
)

最佳答案

这是我对您的问题的解决方案:

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], 1, max)

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,
selection = "single",
filter = 'bottom',
extensions = c('Buttons', 'ColReorder', 'FixedHeader', 'Scroller'),
rownames = FALSE,
options = list(
dom = 'Bfrtip',
searching = T,
pageLength = 25,
searchHighlight = TRUE,
colReorder = TRUE,
fixedHeader = TRUE,
filter = 'top',
buttons = c('copy', 'csv', 'excel', 'print'),
paging = TRUE,
deferRender = TRUE,
scroller = TRUE,
scrollX = TRUE,
scrollY = 550
)
) %>% # Style cells with max_val vector
formatStyle(
columns = 2:5,
backgroundColor = styleEqual(levels = max_val, values = rep("yellow", length(max_val)))
)
})
}
)

所以你需要做的是创建一个最大值向量。然后在辅助函数中使用它 styleEqual()formatStyle()如上面的代码所示。

关于r - DT 数据表 R Shiny 中的条件格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45936714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com