如何加粗最后一行反应表达?

问题描述 投票:0回答:1

问题:

我无法找到一种方法来对反应式表达式table()进行子集化,以便在最后一行加粗。

对于背景,这里是反应式表达式:

 table <- reactive({
  filtered <- service[service$test == input$testcenter,]
  filtered[5, (3:8)] <- colSums(filtered[,3:8])
  filtered$expression[5] <- "Net Total"
  filtered

})

如果我执行以下操作,我会将整个表格加粗。

output$service_table <- DT::renderDataTable(

 if(input$plot.options == "Dollar Amount") {
 table() %>%

 DT::datatable(
      options = list(
           dom = 't',
           digits = 0,
           rownames = FALSE
      )
 ) %>%
 DT::formatCurrency(
      columns = cols,
      currency = "$",
      digits = 0
 ) %>%
 DT::formatRound(
      columns = cols,
      digits = 0
 ) %>%
 DT::formatStyle(
      columns = cols,
      color = DT::styleInterval(0, c('red', 'green'))
 ) %>%
 DT::formatStyle(
      columns = 'metric',#table()$metric,
      valueColumns = 'metric',
      target = 'row',
      fontWeight = 'bold')
})

我想做的就是让最后一行加粗。我尝试了以下所有方法:https://www.r-bloggers.com/dt-an-r-interface-to-the-datatables-library/ https://github.com/rstudio/DT/issues/183 How to mark last row in results of DataTable using R

没有成功。任何关于如何正确地对反应性表达进行子集的见解都将非常受欢迎。

r shiny shinydashboard dt
1个回答
2
投票

你可以用纯CSS做到这一点:

tbody > tr:last-child { 
  font-weight: bold; 
}

将tbody中最后一个tr元素的font-weight设置为粗体。

library(DT)
library(htmltools)
if (interactive()) {
  library(shiny)
  library(DT)
  shinyApp(
    ui = fluidPage(tags$head(tags$style(HTML("tbody > tr:last-child { 
                                               font-weight: bold; 
                                             }"))), fluidRow(column(12, DTOutput('tbl')))),
    server = function(input, output) {
      output$tbl = renderDT(
        iris, options = list(lengthChange = FALSE)
      )
    }
  )
}

enter image description here


如果您只希望数据的最后一行加粗,那么您可以使用一些JavaScript。这里我们硬编码行号150,但你显然可以使用nrow(iris)找到相应的单元格和行。

library(shiny)
library(DT)
shinyApp(
  ui = fluidPage(tags$head(tags$style(HTML("tr>td:contains(150)+tr:last-child { 
                                             font-weight: bold; 
}"))), fluidRow(column(12, DTOutput('tbl')))),
  server = function(input, output) {

    jss <- 'function(setting, json) { 
      $("body").on("DOMNodeInserted", "tr", function() {
        if( $(this).find("td").html() == "150") $(this).css("font-weight", "bold");
      });
    }'

    output$tbl = renderDT(iris, options = list(lengthChange = FALSE, 
                                               initComplete = JS(jss)))
  }
)
© www.soinside.com 2019 - 2024. All rights reserved.