【问题标题】:Copy a Shiny DT cell to users clipboard将闪亮的 DT 单元格复制到用户剪贴板
【发布时间】:2021-02-15 05:05:26
【问题描述】:

基于this 问题:我想在闪亮的数据表中选择一行,并将特定单元格的内容复制到剪贴板。

到目前为止我得到了什么:

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 行后使用此代码,我在剪贴板中获得了以下数据:

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

是否可以去掉表头获取特定单元格的数据(例如disp)?

160

【问题讨论】:

标签: r shiny datatable dt


【解决方案1】:

以下是删除标题和标题的方法:

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 '';",
              "}"
            )
          )
        )
      )
    )
  )
)

【讨论】:

    【解决方案2】:

    为了完整起见,这里有一个来自RStudio Community的修改和缩短的解决方案

    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)
    

    【讨论】:

      猜你喜欢
      • 2018-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多