簡體   English   中英

Shiny中的DT表(覆蓋自定義css)-如何根據特定列中的值為整行着色?

[英]DT table in Shiny (overriding custom css) - How to color the whole row based on a value in a specific column?

我試圖將前兩行的顏色與表格的其余部分不同,因為它包含控制樣本。 該表是使用 DT for Shiny 應用程序制作的。

期望的輸出:

在此處輸入圖像描述

實際輸出:

在此處輸入圖像描述

編碼:

output$table <- renderDataTable({
dt <- dt %>% datatable(rownames=FALSE, class="table table-hover row-border", extensions = c( 'FixedHeader'),
                options = list( scrollX = TRUE, pageLength = -1,dom = 'Btpl', ordering = TRUE, 
                                dom="ft",
                                lengthMenu = list(c(10,25,-1),
                                                  c(10,25,"All")), 
                                columnDefs = list(list(visible=FALSE, targets=c(7,8)))
                )) %>%
          formatStyle('Sample', 
                      fontWeight ='bold', 
                      backgroundColor = styleEqual(c("Positive control", "Negative control"), 
                      c("#fcf4d9", "#fcf4d9")))
})

我嘗試添加target = "row"參數,但它不起作用。 我也嘗試使用styleRow()而不是styleEqual()但這導致錯誤“強制 NA 值”。

更新:

默認的 bakcground 顏色在 tags$style 中指定,這就是為什么它不能使用格式樣式:

  tags$style(HTML(sprintf('table.dataTable tbody tr {background-color:  %1$s !important; color:  %2$s !important;}', 
                          table_col,font_col_dark))

我對這個表達式不太熟悉,但是是否可以在標簽中為控件指定 bakcground 顏色呢?

添加target = 'row'應該是解決方案:

在此處輸入圖像描述

代碼:

# Data
dt <- tibble(Well = 1:5, Sample = c("Positive control", "Negative control", "Sample 1", "Sample 2", "Sample 3"), Result = 10:14)

# Table
dt %>% datatable(rownames=FALSE, class="table table-hover row-border", extensions = c( 'FixedHeader'),
                 options = list( scrollX = TRUE, pageLength = -1,dom = 'Btpl', ordering = TRUE, 
                                 dom="ft",
                                 lengthMenu = list(c(10,25,-1),
                                                   c(10,25,"All")), 
                                 columnDefs = list(list(visible=FALSE, targets=c(7,8)))
                 )) %>%
    formatStyle('Sample', 
                fontWeight ='bold',
                target = 'row',
                backgroundColor = styleEqual(c("Positive control", "Negative control"), 
                                             c("#fcf4d9", "#fcf4d9")))

更新:

您可以通過使用一些輔助向量來控制背景顏色:

# Data
dt <- tibble(Well = 1:5, Sample = c("Positive control", "Negative control", "Sample 1", "Sample 2", "Sample 3"), Result = 10:14)

# Helper
id_helper <- unique(dt$Sample)
color_helper <- ifelse(id_helper %in% c("Positive control", "Negative control"), '#fcf4d9', 'blue')

# Table
dt %>% datatable(rownames=FALSE, class="table table-hover row-border", extensions = c( 'FixedHeader'),
                 options = list( scrollX = TRUE, pageLength = -1,dom = 'Btpl', ordering = TRUE, 
                                 dom="ft",
                                 lengthMenu = list(c(10,25,-1),
                                                   c(10,25,"All")), 
                                 columnDefs = list(list(visible=FALSE, targets=c(7,8)))
                 )) %>%
    formatStyle('Sample', 
                fontWeight ='bold',
                target = 'row',
                backgroundColor = styleEqual(id_helper, 
                                             color_helper))

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM