В R расширение DT KeyTable позволяет перемещаться по ячейкам с помощью клавиш со стрелками.
Можно ли выбрать текущую ячейку с помощью Enter
вместо щелчка мыши?
Заранее спасибо.
library(shiny)
library(DT)
library(datasets)
df <- datasets::mtcars
# Define UI for application that draws a histogram
ui <- fluidPage(
DTOutput('table'), HTML("<br>"),
textOutput('selected')
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$table <- renderDT(
df,
selection = 'single',
extensions = 'KeyTable',
options = list(
dom = 't',
keys = TRUE
)
)
output$selected <- renderText({
print(rownames(df)[input$table_rows_selected])
})
}
# Run the application
shinyApp(ui = ui, server = server)
Чтобы выделить ячейки:
library(shiny)
library(DT)
js <- c(
"table.on('key', function(e, datatable, key, cell, originalEvent){",
" if (key == 13){",
" cell.select();",
" }",
"});"
)
ui <- fluidPage(
DTOutput('table'),
br(),
verbatimTextOutput('selected')
)
server <- function(input, output) {
output$table <- renderDT(
iris,
selection = 'none',
extensions = c("KeyTable", "Select"),
callback = JS(js),
options = list(
dom = 't',
keys = TRUE,
select = list(style = "multi", items = "cell")
),
server = FALSE
)
output$selected <- renderPrint({
input$table_cells_selected
})
}
# Run the application
shinyApp(ui = ui, server = server)
Если вы также хотите отменить выбор:
js <- c(
"table.on('key', function(e, dt, key, cell, originalEvent){",
" if (key == 13){",
" var selected = dt.cells({selected: true});",
" var indices = selected.indexes().toArray().map(JSON.stringify);",
" if (indices.indexOf(JSON.stringify(cell.index())) === -1) {",
" cell.select();",
" } else {",
" cell.deselect();",
" }",
" }",
"});"
)