首页 > 解决方案 > 迭代地突出显示闪亮的渲染表中的一行

问题描述

对于闪亮的应用程序,我想逐行浏览数据框并突出显示(粗体、颜色或类似)中的选定行renderTable。我正在考虑按索引选择行。我可以这样做renderTable,还是应该考虑DT

library(shiny)

ui <- 
  fluidRow(
    actionButton(
      "my_button",
      "Go to next row"
    ),
    tableOutput("my_table")
  )

server <- function(input, output){

  values <- reactiveValues()
  values$index <- 1
  values$dat <- iris

  observeEvent(
    input$my_button, {
      values$index <- values$index + 1
  })

  output$my_table <- 
    renderTable(values$dat) # somehow highlight the row at the index
}

shinyApp(ui = ui, server = server)

标签: htmlrshinydt

解决方案


这可能会让你开始。

library(shiny)
library(DT)
library(dplyr)

ui <- 
  fluidRow(
    actionButton(
      "my_button",
      "Go to next row"
    ),
    dataTableOutput("my_table")
  )

server <- function(input, output){

  values <- reactiveValues()
  values$index <- 1
  values$dat <- iris

  observeEvent(
    input$my_button, {
      values$index <- values$index + 1
    })

  output$my_table <- 
    renderDataTable({
      values$dat %>%
        mutate(row = row_number()) %>%
        datatable() %>% 
        formatStyle(
          "row",
          target = 'row',
          backgroundColor = styleEqual(values$index, c('yellow'))
      )
    }) # somehow highlight the row at the index
}

shinyApp(ui = ui, server = server)

推荐阅读