r - Shiny DT - 使用按钮选择选定行后的行

标签 r shiny dt

到目前为止,我选择了包含文档名称的数据表的一行,并从数据库中获取有关所选文档的信息。此信息的概述位于另一个选项卡中。现在,我喜欢有一个“下一步”按钮来选择所选行之后的行,以显示下一个文档的信息。我无法使用 rownumber+1 因为我的数据表是有序的。有没有办法使用按钮获取所选行之后的行?

更新:

library(DT)
# ui
ui <- tagList(
  ui <- basicPage(
    h2("My Table"),
    DT::dataTableOutput("DT_show_docs"),
    textOutput("printScore"),
    actionButton("next_doc", "Next Document")
  )
)

# Server
server <- function(input, output, session) {
  doc_overview <- reactive({
    df <- data.frame(Doc_ID = seq(1,100), Filename = paste0("File_",seq(1,100)), Score = sample(1:10,100,replace=TRUE), Approved = rep(c("No", "Yes"), times = c(95,5)), Date = rep(seq(as.Date("2018/01/01"), as.Date("2018/1/10"), "days"), length.out=100))
    return(df)
  })

  output$DT_show_docs <- renderDataTable({

    DT::datatable(doc_overview(), 
                  options = list(
                    searching       = FALSE,
                    order           = list(list(3, 'desc'))),
                  selection = "single")

  })

  output$printScore <- renderText({
    row <- input$DT_show_docs_rows_selected
    text <- doc_overview()[row, "Filename"]
  })

  observeEvent(input$next_doc, {
   # Function to select next row/document from datatable. When the button is clicked,
   # and the first row is selected at this moment, I want to select/print the second 
   # row and so on.
  })
}

# app
shinyApp(ui = ui, server = server)

最佳答案

我有一种感觉(可能是错误的)这可能是 XY Problem 的情况。陷阱,即您在尝试解决问题而不是根本问题本身时寻求帮助。如果是这种情况,您最好提出另一个问题来解释您的核心需求,这可能会产生更简单的整体解决方案。虽然我的解决方案非常简单,但整个事情感觉像是一个不必要的复杂化。

无论如何,这是一种方法。您可以使用 input$tableId_rows_all 获取数据表上的当前行顺序,它给出所有页面上的行索引(在通过搜索字符串过滤表之后)。 output$test 实时显示此顺序。现在,您只需在每次用户点击 next_doc 操作按钮时循环执行此顺序即可。

即使您重新排序行或手动更改所选行,此解决方案也将起作用。

library(shiny)
library(DT)

ui <- tagList(
  ui <- basicPage(
    h2("My Table"),
    DT::dataTableOutput("DT_show_docs"),
    textOutput("printScore"),
    actionButton("next_doc", "Next Document"),
    verbatimTextOutput("test")
  )
)

# Server
server <- function(input, output, session) {

  doc_overview <- reactive({
    df <- data.frame(Doc_ID = seq(1,12), 
                     Filename = paste0("File_",seq(1,12)), 
                     Score = sample(1:10,12,replace=TRUE), 
                     Approved = rep(c("No", "Yes"), times = c(5,7)), 
                     Date = rep(seq(as.Date("2018/01/01"), as.Date("2018/1/10"), "days"), length.out=12))
    return(df)
  })

  output$DT_show_docs <- renderDataTable({

    DT::datatable(doc_overview(), 
                  options = list(
                    searching       = FALSE,
                    order           = list(list(3, 'desc'))),
                  selection = "single")

  })

  row_index <- reactiveValues(index = 1)

  observe({ # reset row_index when you manually select any row
    row_index$index <- which(input$DT_show_docs_rows_selected == input$DT_show_docs_rows_all)
  })

  DT_show_docs_proxy <- dataTableProxy("DT_show_docs")

  output$printScore <- renderText({
    row <- input$DT_show_docs_rows_selected
    text <- doc_overview()[row, "Filename"]
  })

  observeEvent(input$next_doc, {
   # Function to select next row/document from datatable. When the button is clicked,
   # and the first row is selected at this moment, I want to select/print the second 
   # row and so on.
    req(input$DT_show_docs_rows_selected) # must select some row before using next_doc button
    row_order <- input$DT_show_docs_rows_all # gives current order of rows
    row_index$index <- isolate(row_index$index) + 1 # cycles throw the order one by one when you click next_button
    selectRows(DT_show_docs_proxy, selected = row_order[row_index$index]) # selects the row of current index
  })

  output$test <- renderPrint({
    input$DT_show_docs_rows_all # shows the order of rows in real time
  })
}

# app
shinyApp(ui = ui, server = server)

关于r - Shiny DT - 使用按钮选择选定行后的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52880234/

相关文章:

r - 在ggplot2中使用geom_violin将统计信息传递给geoms

在多页数据表中注册所有输入

mysql - 使用 R Shiny 中的 rfm 包进行新近度、频率和货币值(value)分析

r - 在数据框中选择带有索引的列和行值

r - 重新排序和子集化时为 ggplot 分配稳定的颜色

miktex 和 pandoc 的相对系统路径 - 打包为 Windows 桌面应用程序的 Shiny 应用程序

r - 如何使用 DT 包中的 renderDataTable() 格式化数据表的列?

javascript - R Shiny : Get DT row background color on top of column background color

r - Purrr ~ 操作符记录在哪里?

javascript - Rhandsontable 条件格式 - 如何根据特定属性值突出显示行?