反复在闪亮的renderTable中突出显示一行

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

对于有光泽的应用程序,我想逐行浏览数据框并突出显示(粗体,彩色或类似)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)
html r shiny dt
1个回答
0
投票

这可能会让您入门。

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)
© www.soinside.com 2019 - 2024. All rights reserved.