闪亮的DT-使用按钮在选定的行之后选择行

时间:2018-10-18 18:19:04

标签: 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)

1 个答案:

答案 0 :(得分:0)

我有种感觉(可能是错误的),这可能是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)