简体   繁体   English

Shiny DT 造型电池 colors

[英]Shiny DT Styling Cell colors

I am attempting to load a file perform various preprocessing steps including subsetting the data.我正在尝试加载一个文件,执行各种预处理步骤,包括对数据进行子集化。 I am doing this all in a single reactive function and then assigning the various formated dataframes to a list to "export" out of the render function. The challenge I am running into when I display the tables renderDT only likes a data frame object, which was ok until I tried to figure out how to color certain cells.我在一个单一的反应 function 中完成这一切,然后将各种格式化的数据帧分配给一个列表以从渲染 function 中“导出”。我在显示表 renderDT 时遇到的挑战只喜欢数据帧 object,它没问题,直到我试图弄清楚如何为某些单元格着色。 All the examples I found for styling cells requires a datatable object not a dataframe. See example: https://rstudio.github.io/DT/010-style.html我找到的所有样式单元格示例都需要数据表 object 而不是 dataframe。请参见示例: https://rstudio.github.io/DT/010-style.html

I am trying to color column C based on the following rules [1,10] = green, [11,19] = yellow, and [20,25] = red.我正在尝试根据以下规则为 C 列着色 [1,10] = 绿色,[11,19] = 黄色,[20,25] = 红色。 I appreciate any suggestions on how to modify renderDT to allowing stlying of cells.我感谢任何关于如何修改 renderDT 以允许单元格移动的建议。 Also does anyone if there is a way to change the font size inside the table?如果有办法更改表格内的字体大小,还有人吗?

require(readxl)
require(openxlsx)

 mydata<-structure(list(A = c(2, 2, 2, 6, 7, 8, 6, 7, 8, 6, 7, 8, 1, 1, 
 1, 1, 2), B = c("A", "A", "N", "O", "L", "L", "O", "L", "L", 
 "O", "L", "L", "A", "N", "N", "N", "A"), C = c(1, 2, 3, 4, 5, 
 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 20, 24)), class = "data.frame", row.names = c(NA, 
 -17L))

 runApp(
  list(
    ui = fluidPage(
      titlePanel("Import QUEST, Design, & Mitigation Lists"),
      sidebarLayout(
        sidebarPanel(
          fileInput('file1', 'Choose QUEST xlsx formatted file',
                    accept = c(".xlsx")
                   )),
        mainPanel(
          tabsetPanel(type = "tab",
                      tabPanel("Tab1", DTOutput('first')),
                      tabPanel("Tab2", DTOutput('second'))
                     )))),
    server = function(input, output){
      mydata1 <- reactive({
        req(input$file1)
        inFile <- input$file1
        mydata<-read_excel(inFile$datapath, 1)
        
        data1<-mydata[which(mydata<3),]
        data2<-mydata[which(mydata>6),]
        quest<-list(data1,data2)
        quest })
        
        output$first <- renderDT({as.data.frame(mydata1()[1])})
        
        output$second <- renderDT({as.data.frame(mydata1()[2])})
      
    }
))
shinyApp(ui, server)

As you said, you need to style a datatable object and pass this object to the renderDT function.如您所说,您需要设置数据表 object 的样式并将此 object 传递给renderDT function。

Any other arguments you want to pass to the renderDT now need to be moved to the DT::datatable constructor, see examples in this other post .您想要传递给 renderDT 的任何其他renderDT现在需要移至DT::datatable构造函数,请参阅其他帖子中的示例。

For font-size, see also this other post .对于字体大小,另请参阅其他帖子

You have to pass a datatable in renderDT :您必须在datatable中传递一个renderDT

output$first <- renderDT({
  datatable(as.data.frame(mydata1()[1])) %>% 
      formatStyle(
        columns = "C", 
        backgroundColor = styleInterval(c(1,11,20), c("white", "red", "green", "yellow"))
      )
})

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

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