R闪亮DT styleEqual()显示不正确的颜色

时间:2018-08-07 20:37:43

标签: r shiny dt

我正在使用DT包来设置闪亮应用程序中表格中单元格的背景颜色。我正在使用styleEqual()函数来提供需要表格背景的颜色向量。但是,色彩元素的顺序正变得不适当,并且我不知道原因/方式。下面是我的ui.R和server.R的简化复制示例,其中显示了DT色表。在表格(summary_tab)中,第四列“ T2”值得注意的是-第5行和第6行应具有相同的颜色。当我复制一张常规表(testcols)以显示此列中的颜色顺序(我希望显示的颜色顺序)时,似乎颜色顺序是适当的。我很困惑如何解决此问题。谢谢您的帮助。

UI:

tagList(
  navbarPage(
    theme = shinythemes::shinytheme("sandstone"),
    "Amino_Acid_Modification",
    tabPanel("Mutation-Level Analysis",
             fluidRow(
               column(12, offset=0,
                      DT::dataTableOutput('summary_tab'),
                      tableOutput("testcols")
             )
    )
  )
)
)

服务器:

library(DT)
shinyServer(function(input, output, session) {

  summary_Table <- reactive({
    Position = c(391,391,2636,2636,391,391)
    chng_type = c("-","+", "-", "+", "-", "+")
    T1 = c(11.68, 7.19, 5.95,2.00, 1.36, 7.19)
    T2 = c(4.29, 1.28, 5.19,2.28, 0.23, 1.28)
    T3 = c(30.61, 11.74, 1.77,0.45, 3.09, 11.74)
    T10 = c(0.81, 1.13, 0.71,0.69, 1.31, 1.13)
    T11 = c(13.42, 6.71, 13.79,3.91, 0.63, 6.71)
    T13 = c(4.58, 1.82, 5.98,3.21, 0.54, 1.82)
    T14 = c(0.49, 0.18, 3.43,1.26, 0.05, 0.18)
    T16 = c(1.46, 0.84, 2.99,1.32, 0.19, 0.84)
    mut = c("Val390Val", "Val390Val", "Ser2639Pro","Ser2639Pro", "Pro394Arg", "Pro394Arg")

    df<-data.frame(Position, chng_type, T1, T2, T3, T10, T11, T13, T14, T16, mut)
    df
      })

  summary_Colors <- reactive({
    label.vector = c("#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF")
    T1 = c("#FFC2C2", "#FFC2C2", "#FFC9C9","#FFC9C9", "#FFB0B0", "#FFB0B0")
    T2 = c("#FFD6D6", "#FFD6D6", "#FFD7D7","#FFD7D7", "#FFF0F0", "#FFF0F0")
    T3 = c("#FF0000", "#FF0000", "#FFEDED","#FFEDED", "#FF8A8A", "#FF8A8A")
    T10 = c("#FFFAFA", "#FFFAFA", "#FFFEFE","#FFFEFE", "#FFFCFC", "#FFFCFC")
    T11 = c("#FFA4A4", "#FFA4A4", "#FF7979","#FF7979", "#FFACAC", "#FFACAC")
    T13 = c("#FFD9D9", "#FFD9D9", "#FFD9D9","#FFD9D9", "#FFEDED", "#FFEDED")
    T14 = c("#FFFAFA", "#FFFAFA", "#FFE1E1","#FFE1E1", "#FFFDFD", "#FFFDFD")
    T16 = c("#FFF6F6", "#FFF6F6", "#FFE8E8","#FFE8E8", "#FFF6F6", "#FFF6F6")
    df<-data.frame(label.vector, T1, T2, T3, T10, T11, T13, T14, T16, label.vector)
    df<-as.matrix(df)
    df
        })

  output$summary_tab = DT::renderDT({
    datatable(summary_Table()) %>% 
      formatStyle('T1', backgroundColor = styleEqual( summary_Table()[,3], summary_Colors()[,3])) %>%
      formatStyle('T2', backgroundColor = styleEqual( summary_Table()[,4], summary_Colors()[,4])) %>%
      formatStyle('T3', backgroundColor = styleEqual( summary_Table()[,5], summary_Colors()[,5])) %>%
      formatStyle('T10', backgroundColor = styleEqual( summary_Table()[,6], summary_Colors()[,6])) %>%
      formatStyle('T11', backgroundColor = styleEqual( summary_Table()[,7], summary_Colors()[,6])) %>%
      formatStyle('T13', backgroundColor = styleEqual( summary_Table()[,8], summary_Colors()[,7])) %>%
      formatStyle('T14', backgroundColor = styleEqual( summary_Table()[,9], summary_Colors()[,9])) %>%
      formatStyle('T16', backgroundColor = styleEqual( summary_Table()[,10], summary_Colors()[,10])) 

  })

  output$testcols = renderTable(
    data.frame(summary_Table()[,4], summary_Colors()[,4])

  )
}
)

0 个答案:

没有答案