【问题标题】:Iteratively highlight a row in shiny renderTable迭代地突出显示闪亮的渲染表中的一行
【发布时间】:2020-08-11 04:11:38
【问题描述】:

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

【问题讨论】:

  • 使用DT 可能更容易,因为突出显示行/列的功能已经到位。例如:rstudio.github.io/DT/010-style.html。如果你想得到一个“普通”的表格,没有分页、过滤、排序、搜索……,你可以使用options = list(dom = "t")

标签: html r shiny dt


【解决方案1】:

这可能会让你开始。

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)

【讨论】:

    猜你喜欢
    • 2014-09-04
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-12
    • 1970-01-01
    • 2016-12-29
    相关资源
    最近更新 更多