简体   繁体   English

将 Shiny DT 单元格复制到用户剪贴板

[英]Copy a Shiny DT cell to users clipboard

Based on this question: I want to select a row in a shiny datatable and want to copy the content of a specific cell to the clipboard.基于这个问题:我想在闪亮的数据表中选择一行,并想将特定单元格的内容复制到剪贴板。

What I've got so far:到目前为止我所得到的:

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)

With this code after selecting row 1 I got following data in the clipboard:使用此代码选择第 1 行后,我在剪贴板中获得了以下数据:

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

Is it possible to remove the header and get the data of a specific cell (eg disp)?是否可以删除标题获取特定单元格的数据(例如 disp)?

160

Here is how to remove the title and the 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 '';",
              "}"
            )
          )
        )
      )
    )
  )
)

For the sake of completeness here a modified and shortened solution from the RStudio Community为了完整起见,这里有一个来自RStudio 社区的修改和缩短的解决方案

library(shiny)
library(DT)
library(rclipboard)

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM