闪亮的DT表中的限制行选择 [英] Limit row selection in DT Table in Shiny
问题描述
我目前正在尝试将我在Shiny中的数据表中的选择限制为只有两行-我希望该表不允许用户单击多于几行(但也可以在以后取消选择它们)。 / p>
I am currently trying to limit my selection in a DataTable in Shiny to just two rows - I want the table to not allow the user to click on more than rows (but also to have the ability to deselect them afterwards).
library(DT)
shinyApp(
ui = fluidPage(
fluidRow(
column(12,
dataTableOutput('table')
)
)
),
server = function(input, output) {
output$table <- DT::renderDataTable(iris,
options = list(selection = "multiple")
)
}
)
行选择当前处于多种模式,可以使用,但是我不希望选择超过两行。
The row selection is currently on multiple mode, which works, but I don't want the selection to exceed two rows.
推荐答案
您可以通过已经解决过的javascript来解决它:
限制行选择到数据表中的3
You could either solve it via javascript, which you may have seen already: Limit row selection to 3 in datatables
或者您在Shiny中更新数据表:
Or you update the datatable in 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
}
})
}
)
此解决方案可能不太干净,但更容易理解。
This solution might be less clean, but a bit easier to understand.
这篇关于闪亮的DT表中的限制行选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!