R 闪亮DT-使用按钮在选定行之后选择行
到目前为止,我选择了一行包含文档名称的datatable,并从数据库中获取关于所选文档的信息。此信息的概述在另一个选项卡中。现在,我想在选中后的行旁边有一个按钮来选择,以显示下一个文档的信息。我无法使用行号+1,因为我的数据表已排序。有没有一种方法可以通过使用按钮来获取我所选行之后的行 更新:R 闪亮DT-使用按钮在选定行之后选择行,r,shiny,dt,R,Shiny,Dt,到目前为止,我选择了一行包含文档名称的datatable,并从数据库中获取关于所选文档的信息。此信息的概述在另一个选项卡中。现在,我想在选中后的行旁边有一个按钮来选择,以显示下一个文档的信息。我无法使用行号+1,因为我的数据表已排序。有没有一种方法可以通过使用按钮来获取我所选行之后的行 更新: 我有一种感觉可能是错误的,这可能是一个陷阱的情况,即你在寻求帮助,试图解决一个问题,而不是潜在的问题本身。如果是这种情况,你最好问另一个问题来解释你的核心需求,这可能会产生一个更简单的整体解决方案。虽然我
我有一种感觉可能是错误的,这可能是一个陷阱的情况,即你在寻求帮助,试图解决一个问题,而不是潜在的问题本身。如果是这种情况,你最好问另一个问题来解释你的核心需求,这可能会产生一个更简单的整体解决方案。虽然我的解决方案非常简单,但整个事情感觉像是一个不必要的复杂问题 无论如何,这里有一个方法可以做到这一点。您可以使用输入$tableId\u rows\u all获取数据表上的当前行顺序,该输入提供了在表被搜索字符串过滤后所有页面上的行索引。输出$test实时显示此顺序。现在,您只需在用户每次点击下一个文档操作按钮时循环浏览此订单 即使您对行重新排序或手动更改所选行,此解决方案也会起作用
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逻辑即可。好的,太好了!非常感谢。
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)