[英]Conditional formatting in DT Data Table R Shiny
I have a table with 5 cols and 1st column as character and other four as numeric. 我有一个表有5个cols和第1列作为字符,其他四个作为数字。 I am using DT Data Table to display the same in Shiny App. 我正在使用DT数据表在Shiny App中显示相同的内容。 Now, I need to compare each of the four cols for each row and color code the row cell which has maximum value . 现在,我需要比较每行的四个列中的每一个,并对具有最大值的行单元进行颜色编码 。 Looking for ways to do the same. 寻找方法来做同样的事情。 Had a look on this link as well StylingCells but all the cols are numeric here. 看看这个链接以及StylingCells,但所有的cols都是数字。
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
)
)
})
}
)
Here is my solution to your problem: 以下是我的问题解决方案:
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)))
)
})
}
)
So what you need to do is to create a vector of max values. 所以你需要做的是创建一个最大值的向量。 Then use it in the helper function styleEqual()
inside formatStyle()
as shown in the code above. 然后在formatStyle()
里面的辅助函数styleEqual()
使用它,如上面的代码所示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.