【问题标题】:Shiny DT Styling Cell colors闪亮的 DT 造型单元颜色
【发布时间】:2021-10-16 01:05:49
【问题描述】:

我正在尝试加载文件执行各种预处理步骤,包括对数据进行子集化。我在一个单一的反应函数中完成这一切,然后将各种格式化的数据帧分配给一个列表,以便从渲染函数中“导出”。我在显示表格时遇到的挑战 renderDT 只喜欢一个数据框对象,在我试图弄清楚如何为某些单元格着色之前,这是可以的。我发现的所有样式单元格示例都需要数据表对象而不是数据框。参见示例:https://rstudio.github.io/DT/010-style.html

我正在尝试根据以下规则为 C 列着色 [1,10] = 绿色、[11,19] = 黄色和 [20,25] = 红色。我很欣赏有关如何修改 renderDT 以允许单元格的任何建议。如果有办法改变表格内的字体大小,还有人吗?

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)

【问题讨论】:

    标签: r shiny styling dt


    【解决方案1】:

    如您所说,您需要设置数据表对象的样式并将该对象传递给renderDT 函数。

    您想要传递给renderDT 的任何其他参数现在都需要移至DT::datatable 构造函数,请参阅this other post 中的示例。

    有关字体大小,另请参阅this other post

    【讨论】:

      【解决方案2】:

      你必须在renderDT中传递一个datatable

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

      【讨论】:

        猜你喜欢
        • 2016-02-18
        • 2019-09-26
        • 2021-11-15
        • 2018-11-20
        • 2021-10-02
        • 2016-10-25
        • 2020-09-18
        • 2018-08-04
        • 2016-01-15
        相关资源
        最近更新 更多