简体   繁体   English

基于值的R Shiny DT格式化列

[英]R Shiny DT Formatting Columns Based on Values

I'm trying to format the output of multiple columns based on another column. 我正在尝试根据另一列来格式化多列的输出。

In the example below I want to format Col1/2/3/4 based on their values in comparison to the value in the column 'Argu'. 在下面的示例中,我想根据与“ Argu”列中的值相比,它们的值来格式化Col1 / 2/3/4。

ie
- if Col1, Row1 is less than 7 mark Red, if not Green. -如果Col1,则Row1小于7标记为红色,否则为绿色。
- if Col1, Row2 is less than 6 mark Red, if not Green. -如果Col1,Row2小于6标记为红色,否则为绿色。
- if Col1, Row3 is less than 7 mark Red, if not Green. -如果Col1,Row3小于7标记为红色,否则为绿色。
- if Col2, Row1 is less than 7 mark Red, if not Green. -如果是Col2,则Row1小于7标记为红色,否则为绿色。
- if Col2, Row2 is less than 6 mark Red, if not Green. -如果Col2,则Row2小于6标记为红色,否则为绿色。
- if Col2, Row3 is less than 7 mark Red, if not Green. -如果Col2,则Row3小于7标记为红色,否则为绿色。

etc. 等等

I know is straightforward to format one column based on another column by explicitly calling out that value but for changeable argument values between rows this does not work 我知道直接通过显式调用该值来格式化另一列的格式很简单,但是对于行之间可变的参数值却不起作用

# Load Packages
library(shiny)
library(DT)

# Create DT
Col1 = c(9,5,8)
Col2 = c(9,4,7)
Col3 = c(9,9,5)
Col4 = c(8,8,7)
Argu = c(7,6,7)
df = data.frame(Col1,Col2,Col3,Col4,Argu)

# Create Shiny Output
shinyApp(
  ui = 
      navbarPage("Testing",dataTableOutput('dt')),

  server = function(input, output, session) {

    output$dt = DT::renderDataTable(datatable(df)
    %>% formatStyle("Col1","Argu", backgroundColor = styleInterval(6.1,
c("Red","Green")))

  )}
 )

Any help would be appreciated! 任何帮助,将不胜感激!

You can build hidden logical columns to compare each data column with Argu and format the columns on DT accordingly. 您可以构建隐藏的逻辑列,以将每个数据列与Argu进行比较,并相应地在DT上格式化列。

Please refer to the code below. 请参考下面的代码。 Hope this helps. 希望这可以帮助。

# Load Packages
library(shiny)
library(DT)

# Create DT
Col1 = c(9,5,8)
Col2 = c(9,4,7)
Col3 = c(9,9,5)
Col4 = c(8,8,7)
Argu = c(7,6,7)
df = data.frame(Col1,Col2,Col3,Col4,Argu)

# Build hidden logical columns for conditional formatting
dataCol_df <- ncol(df) - 1
dataColRng <- 1:dataCol_df
argColRng <- (dataCol_df + 2):(dataCol_df * 2 + 1)
df[, argColRng] <- df[, dataColRng] < Argu

# Create Shiny Output
shinyApp(
  ui = 
    navbarPage("Testing",dataTableOutput('dt')),
  server = function(input, output, session) {
    output$dt = DT::renderDataTable(
      datatable(df,
                # Hide logical columns
                options=list(columnDefs = list(list(visible=FALSE, 
                                                    targets=argColRng)))) %>% 
        # Format data columns based on the values of hidden logical columns
        formatStyle(columns = dataColRng,
                    valueColumns = argColRng,
                    backgroundColor = styleEqual(c('0', '1'), 
                                                 c("green", "red")))
    )}
)

Here's another solution: 这是另一个解决方案:

library(shiny)
library(DT)

# Create DT
Col1 = c(9,5,8)
Col2 = c(9,4,7)
Col3 = c(9,9,5)
Col4 = c(8,8,7)
Argu = c(7,6,7)
df = data.frame(Col1,Col2,Col3,Col4,Argu)

# Create Shiny Output
shinyApp(
  ui = 
    navbarPage("Testing",dataTableOutput('dt')),

  server = function(input, output, session) {

   output$dt <- renderDataTable( DT::datatable(df,options = list(rowCallback = JS('
                                                          function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
                                                     if (parseFloat(aData[1]) < aData[5])
                                                     $("td:eq(1)", nRow).css("background-color", "red");
                                                     if (parseFloat(aData[1]) >= aData[5])
                                                     $("td:eq(1)", nRow).css("background-color", "green");
                                                     if (parseFloat(aData[2]) < aData[5])
                                                     $("td:eq(2)", nRow).css("background-color", "red");
                                                     if (parseFloat(aData[2]) >= aData[5])
                                                     $("td:eq(2)", nRow).css("background-color", "green");
                                                     if (parseFloat(aData[3]) < aData[5])
                                                     $("td:eq(3)", nRow).css("background-color", "red");
                                                     if (parseFloat(aData[3]) >= aData[5])
                                                     $("td:eq(3)", nRow).css("background-color", "green");
                                                     if (parseFloat(aData[4]) < aData[5])
                                                     $("td:eq(4)", nRow).css("background-color", "red");
                                                     if (parseFloat(aData[4]) >= aData[5])
                                                     $("td:eq(4)", nRow).css("background-color", "green");
                                                                                  }'
                                                                                  ))))


    }
)

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

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