迭代突出闪亮的 renderTable 中的一行

Iteratively highlight a row in shiny renderTable

对于闪亮的应用程序,我想逐行浏览数据框并突出显示(粗体、颜色或类似)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)

这可能会让您入门。

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)