在 Shiny 中限制 DT Table 中的行选择
Limit row selection in DT Table in Shiny
我目前正试图将我在 Shiny 中的 DataTable 中的选择限制为只有两行 - 我希望 table 不允许用户单击多行(但也有能力之后取消选择它们)。
library(DT)
shinyApp(
ui = fluidPage(
fluidRow(
column(12,
dataTableOutput('table')
)
)
),
server = function(input, output) {
output$table <- DT::renderDataTable(iris,
options = list(selection = "multiple")
)
}
)
行选择当前处于多行模式,该模式有效,但我不希望选择超过两行。
更新:自 04.2022 或更早版本以来似乎不再有效。
您可以通过 javascript 解决它,您可能已经看到了:
Limit row selection to 3 in datatables
或者您在 Shiny 中更新数据表:
library(DT)
library(shiny)
shinyApp(
ui = fluidPage(
fluidRow(
column(12,dataTableOutput('tbl'))
)
),
server = function(input, output) {
reset <- reactiveValues(sel = "")
output$tbl <- DT::renderDataTable({
input$tbl_rows_selected
datatable(iris, selection = list(mode = 'multiple', selected = reset$sel))
})
observe({
if(length(input$tbl_rows_selected) > 2){
reset$sel <- setdiff(input$tbl_rows_selected, input$tbl_row_last_clicked)
}else{
reset$sel <- input$tbl_rows_selected
}
})
}
)
此解决方案可能不太干净,但更容易理解。
我目前正试图将我在 Shiny 中的 DataTable 中的选择限制为只有两行 - 我希望 table 不允许用户单击多行(但也有能力之后取消选择它们)。
library(DT)
shinyApp(
ui = fluidPage(
fluidRow(
column(12,
dataTableOutput('table')
)
)
),
server = function(input, output) {
output$table <- DT::renderDataTable(iris,
options = list(selection = "multiple")
)
}
)
行选择当前处于多行模式,该模式有效,但我不希望选择超过两行。
更新:自 04.2022 或更早版本以来似乎不再有效。
您可以通过 javascript 解决它,您可能已经看到了: Limit row selection to 3 in datatables
或者您在 Shiny 中更新数据表:
library(DT)
library(shiny)
shinyApp(
ui = fluidPage(
fluidRow(
column(12,dataTableOutput('tbl'))
)
),
server = function(input, output) {
reset <- reactiveValues(sel = "")
output$tbl <- DT::renderDataTable({
input$tbl_rows_selected
datatable(iris, selection = list(mode = 'multiple', selected = reset$sel))
})
observe({
if(length(input$tbl_rows_selected) > 2){
reset$sel <- setdiff(input$tbl_rows_selected, input$tbl_row_last_clicked)
}else{
reset$sel <- input$tbl_rows_selected
}
})
}
)
此解决方案可能不太干净,但更容易理解。