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