2017-08-07 49 views
2

我使用DT库以可视化的表,但让我们说,我想给一个颜色,从第1行像例如RED一些行至第4行:如何给DT表的给定间隔的行赋予颜色?

enter image description here

而且这将是非常好的如果可能的话改变文本的颜色。经过搜索的时间,我发现从库DT此功能:

datatable(df, rownames = FALSE) %>% 
    formatStyle(columns = "inputval", 
       background = styleInterval(c(0.7, 0.8, 0.9)-1e-6, c("white", "lightblue", "magenta", "white"))) 

但我需要给皮肤上色的所有列不就像在代码inputval选定的专栏中,我可以给columns值类似names(df)所以可以给所有列添加颜色?并且styleInterval选择表格中的值而不是行的间隔,我该怎么做才能选择行并给它们一个颜色?

回答

3

像这样的东西应该可以完成这项工作。请注意,我有色行2:4的目的,而不是1:4的更多的功能:

library(shiny) 
library(DT) 

ui <- basicPage(
    mainPanel(DT::dataTableOutput('mytable')) 
) 

server <- function(input, output,session) { 

    output$mytable = DT::renderDataTable( 
    DT::datatable(mtcars, options = list(
     pageLength = 25, 
     rowCallback = JS('function(row, data, index, rowId) {', 
         'console.log(rowId)','if(rowId >= 1 && rowId < 4) {', 
         'row.style.backgroundColor = "pink";','}','}') 
    ) 
    ) 
) 




} 
runApp(list(ui = ui, server = server)) 

enter image description here

编辑:动态色彩行:在这里我只是用sub来取代范围以色行

library(shiny) 
library(DT) 

fnc <- JS('function(row, data, index, rowId) {', 
        'console.log(rowId)','if(rowId >= ONE && rowId < TWO) {', 
        'row.style.backgroundColor = "pink";','}','}') 

ui <- basicPage(
    sliderInput("colorrows", "Which to color:",min = 0, max = 10, value = c(1,3)), 
    mainPanel(DT::dataTableOutput('mytable')) 
) 

server <- function(input, output,session) { 

    Coloring <- eventReactive(input$colorrows,{ 
    fnc <- sub("ONE",input$colorrows[1],fnc) 
    fnc <- sub("TWO",input$colorrows[2],fnc) 
    fnc 
    }) 

    output$mytable = DT::renderDataTable(
    DT::datatable(mtcars, options = list(pageLength = 25,rowCallback = Coloring()) 
    ) 
) 
} 
runApp(list(ui = ui, server = server)) 

enter image description here

+0

谢谢你的回答,BU我有一个问题,行是用户输入是否可以传递给他们rollCallBack参数,如'if(rowId> = input $ fromRow && rowId

+0

查看上面的编辑,让我知道你是否有任何问题 –