[英]DT::datatable - Select rows for deletion and write csv WITHOUT Shiny
[英]How to select max number of cells in shiny DT datatable
有沒有辦法 select 在 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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.