将 Shiny DT 单元格复制到用户剪贴板
Copy a Shiny DT cell to users clipboard
基于问题:我想select闪亮数据表中的一行,并想将特定单元格的内容复制到剪贴板。
到目前为止我得到了什么:
library(DT)
ui <- basicPage(
h2("The mtcars data"),
DT::dataTableOutput("mytable")
)
server <- function(input, output) {
output$mytable <- DT::renderDataTable({
DT::datatable(mtcars,
rownames = FALSE,
extensions = c("Buttons", "Select"),
selection = 'none',
options =
list(
select = TRUE,
dom = "Bfrtip",
buttons = list(
list(
extend = "copy",
text = 'Copy',
exportOptions = list(modifier = list(selected = TRUE))
)
)
))
})
}
shinyApp(ui, server)
在第 1 行 select 之后使用此代码,我在剪贴板中获得了以下数据:
Exported data
mpg cyl disp hp drat wt qsec vs am gear carb
21 6 160 110 3.9 2.62 16.46 0 1 4 4
是否可以删除 header 和 获取特定单元格的数据(例如 disp)?
160
以下是删除标题和header的方法:
datatable(
iris,
rownames = FALSE,
extensions = c("Buttons", "Select"),
options = list(
select = TRUE,
dom = "Bfrtip",
buttons = list(
list(
extend = "copy",
text = "Copy",
title = NULL,
exportOptions = list(
modifier = list(selected = TRUE),
format = list(
header = JS(
"function(text, index, node) {",
" return '';",
"}"
)
)
)
)
)
)
)
为了完整起见,这里修改并缩短了 RStudio Community
的解决方案
library(shiny)
library(DT)
ui <- fluidPage(
fluidRow(
DT::dataTableOutput(outputId = "my_data_table")
)
)
server <- function(input, output) {
shinyInput <- function(FUN, len, id, ...) {
inputs <- character(len)
for (i in seq_len(len)) {
inputs[i] <- as.character(FUN(paste0(id, i), ...))
}
inputs
}
my_data_table <- reactive({
data.frame(
mtcars,
Actions = shinyInput(actionButton, nrow(mtcars),
'button_',
label = "clipboard",
onclick = paste0('Shiny.onInputChange( \"select_button\" , this.id)')
)
)
})
output$my_data_table <- renderDataTable({
my_data_table()
}, escape = FALSE)
observeEvent(input$select_button, {
selectedRow <- as.numeric(strsplit(input$select_button, "_")[[1]][2])
writeClipboard(as.character(my_data_table()[selectedRow,3]))
})
}
shinyApp(ui, server)
这是使用 rclipboard 包的方法:
library(shiny)
library(DT)
library(rclipboard)
ui <- fluidPage(
rclipboardSetup(),
br(),
fluidRow(
column(
width = 9,
DTOutput("dtable")
),
column(
width = 3,
tags$h2("Try to paste here:"),
tags$textarea()
)
)
)
server <- function(input, output, session) {
my_data_table <- mtcars
my_data_table[["Action"]] <- vapply(1L:nrow(my_data_table), function(i){
as.character(
rclipButton(
paste0("clipbtn_", i),
label = "Copy",
clipText = my_data_table[i, "disp"],
icon = icon("copy", lib = "glyphicon"),
class = "btn-primary btn-sm"
)
)
}, character(1L))
output[["dtable"]] <- renderDT({
datatable(
my_data_table,
escape = FALSE,
selection = "none",
options = list(
columnDefs = list(
list(targets = ncol(my_data_table), orderable = FALSE)
)
)
)
})
}
shinyApp(ui, server)
基于
到目前为止我得到了什么:
library(DT)
ui <- basicPage(
h2("The mtcars data"),
DT::dataTableOutput("mytable")
)
server <- function(input, output) {
output$mytable <- DT::renderDataTable({
DT::datatable(mtcars,
rownames = FALSE,
extensions = c("Buttons", "Select"),
selection = 'none',
options =
list(
select = TRUE,
dom = "Bfrtip",
buttons = list(
list(
extend = "copy",
text = 'Copy',
exportOptions = list(modifier = list(selected = TRUE))
)
)
))
})
}
shinyApp(ui, server)
在第 1 行 select 之后使用此代码,我在剪贴板中获得了以下数据:
Exported data
mpg cyl disp hp drat wt qsec vs am gear carb
21 6 160 110 3.9 2.62 16.46 0 1 4 4
是否可以删除 header 和 获取特定单元格的数据(例如 disp)?
160
以下是删除标题和header的方法:
datatable(
iris,
rownames = FALSE,
extensions = c("Buttons", "Select"),
options = list(
select = TRUE,
dom = "Bfrtip",
buttons = list(
list(
extend = "copy",
text = "Copy",
title = NULL,
exportOptions = list(
modifier = list(selected = TRUE),
format = list(
header = JS(
"function(text, index, node) {",
" return '';",
"}"
)
)
)
)
)
)
)
为了完整起见,这里修改并缩短了 RStudio Community
的解决方案library(shiny)
library(DT)
ui <- fluidPage(
fluidRow(
DT::dataTableOutput(outputId = "my_data_table")
)
)
server <- function(input, output) {
shinyInput <- function(FUN, len, id, ...) {
inputs <- character(len)
for (i in seq_len(len)) {
inputs[i] <- as.character(FUN(paste0(id, i), ...))
}
inputs
}
my_data_table <- reactive({
data.frame(
mtcars,
Actions = shinyInput(actionButton, nrow(mtcars),
'button_',
label = "clipboard",
onclick = paste0('Shiny.onInputChange( \"select_button\" , this.id)')
)
)
})
output$my_data_table <- renderDataTable({
my_data_table()
}, escape = FALSE)
observeEvent(input$select_button, {
selectedRow <- as.numeric(strsplit(input$select_button, "_")[[1]][2])
writeClipboard(as.character(my_data_table()[selectedRow,3]))
})
}
shinyApp(ui, server)
这是使用 rclipboard 包的方法:
library(shiny)
library(DT)
library(rclipboard)
ui <- fluidPage(
rclipboardSetup(),
br(),
fluidRow(
column(
width = 9,
DTOutput("dtable")
),
column(
width = 3,
tags$h2("Try to paste here:"),
tags$textarea()
)
)
)
server <- function(input, output, session) {
my_data_table <- mtcars
my_data_table[["Action"]] <- vapply(1L:nrow(my_data_table), function(i){
as.character(
rclipButton(
paste0("clipbtn_", i),
label = "Copy",
clipText = my_data_table[i, "disp"],
icon = icon("copy", lib = "glyphicon"),
class = "btn-primary btn-sm"
)
)
}, character(1L))
output[["dtable"]] <- renderDT({
datatable(
my_data_table,
escape = FALSE,
selection = "none",
options = list(
columnDefs = list(
list(targets = ncol(my_data_table), orderable = FALSE)
)
)
)
})
}
shinyApp(ui, server)