首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >闪亮DT -使用按钮在选定行后选择行

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

Stack Overflow用户
提问于 2018-10-18 18:19:04
回答 1查看 1.2K关注 0票数 1

到目前为止,我选择了包含文档名称的datatable的一行,并从数据库中获取有关所选文档的信息。此信息的概述位于另一个选项卡中。现在,我希望有一个按钮" next“来选择选中后的行,以显示下一个文档的信息。我不能使用rownumber+1,因为我的数据表是有序的。是否有一种方法可以通过使用按钮在选定行之后获取行?

更新:

代码语言:javascript
运行
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-19 14:26:53

我有一种感觉(可能是错的),这可能是一个XY问题陷阱的情况,也就是说,你是在寻求帮助,试图解决一个问题,而不是潜在的问题本身。如果是这样的话,最好再问一个问题来解释您的核心需求,这可能会产生一个更简单的整体解决方案。虽然我的解决方案很简单,但整个过程就像是一个不必要的复杂问题。

不管怎样,这有个办法。您可以使用input$tableId_rows_all获取数据表上行的当前顺序,它给出了所有页面上的行的索引(在表被搜索字符串过滤后)。output$test实时显示了这个顺序.现在,您只需要在每次用户点击next_doc操作按钮时循环这个顺序。

即使您重新排序您的行或手动更改选定的行,此解决方案也会工作。

代码语言:javascript
运行
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52880234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档