gpt4 book ai didi

javascript - 如何选择 Shiny 的DT数据表中的最大单元格数

转载 作者:行者123 更新时间:2023-12-02 22:39:36 25 4
gpt4 key购买 nike

有没有办法在 Shiny 的 DT 数据表中选择最多一定数量的单元格?我说的是单元格而不是行。

library(shiny)
if (packageVersion('DT') < '0.1.3') devtools::install_github('rstudio/DT')
library(DT)
shinyApp(
ui = fluidPage(
fluidRow(
h1('Client-side processing'),
DT::dataTableOutput('x1')

)
),
server = function(input, output, session) {
output$x1 = DT::renderDataTable(
iris, server = FALSE,
selection = list(mode = 'multiple',target="cell")
)

}
)

最佳答案

是的,在 Select 扩展的帮助下:

library(shiny)
library(DT)

callback <- c(
"var tblID = $(table.table().node()).closest('.datatables').attr('id');",
"var inputName = tblID + '_cells_selected:DT.cellInfo'",
"",
"table.on('select', function(e, dt, type, ix){",
" var selected = dt.cells({selected: true});",
## send selected cells to Shiny
" var indices = selected.indexes().toArray()",
" .map(function(x){return {row: x.row+1, column: x.column};});",
" Shiny.setInputValue(inputName, indices);",
## deselect if more than 5 selected
" if(selected.count() > 5){",
" dt.cells(ix).deselect();",
" }",
"});",
"",
# on deselect, also send selected cells to Shiny
"table.on('deselect', function(e, dt, type, ix){",
" var selected = dt.cells({selected: true});",
" var indices = selected.indexes().toArray()",
" .map(function(x){return {row: x.row+1, column: x.column};});",
" Shiny.setInputValue(inputName, indices);",
"});"
)

ui <- fluidPage(
br(),
DTOutput("tbl"),
br(),
h3("Selected cells:"),
verbatimTextOutput("selectedCells")
)

server <- function(input, output, session){

output[["tbl"]] <- renderDT({
datatable(
iris[1:6,],
selection = "none",
extensions = "Select",
callback = JS(callback),
options = list(
select = list(style = "multi", items = "cell")
)
)
})

output[["selectedCells"]] <- renderPrint({
input[["tbl_cells_selected"]]
})

}

shinyApp(ui, server)

关于javascript - 如何选择 Shiny 的DT数据表中的最大单元格数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58628043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com