【问题标题】:Shiny DT - Select row after selected row by using a buttonShiny DT - 使用按钮在选定行之后选择行
【发布时间】:2018-10-18 18:19:04
【问题描述】:

到目前为止,我选择了包含文档名称的数据表的一行,并从数据库中获取有关所选文档的信息。此信息的概述在另一个选项卡中。现在,我喜欢有一个按钮“下一步”来选择选中后的行,以显示下一个文档的信息。我不能使用 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)

【问题讨论】:

  • 我可以帮助你,但我需要你提供一个绝对最小但完全可重现的应用程序代码。请更新您的问题,我会回答。
  • 我添加了一个最小且完整的应用程序代码。提前谢谢!!!

标签: r shiny dt


【解决方案1】:

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

【讨论】:

  • 非常感谢!!!也许是因为我的英语有限,但我认为这不是一个XY问题陷阱。我有一张包含文件列表的表格。我想从此表中选择一个文档以查看有关此文档的信息,然后我想单击下一步按钮以从表中获取下一个文档(按特定字段排序)。但我认为这与我原来的问题相同。
  • 你的英语很好! :)。表格是否总是按分数降序排序,还是用户可以按照他们想要的任何方式对其进行排序?如果总是按分数降序排序,那么有一种更简单的方法可以满足您的要求。
  • 我还没有决定。也许我想保持灵活性。如果我始终保持表格按分数降序排序,解决方案是什么?
  • 在这种情况下,只需在 doc_overview 响应式中对您的数据进行预排序,并在用户点击 next_doc 按钮时简单地使用 rownumber+1 逻辑。
  • 好的,完美!谢谢。
猜你喜欢
  • 2018-06-22
  • 1970-01-01
  • 2021-03-25
  • 2021-01-16
  • 2015-09-13
  • 2017-09-30
  • 2021-08-22
  • 2017-01-09
  • 1970-01-01
相关资源
最近更新 更多