【问题标题】:How to add custom button in R Shiny datatable?如何在 R Shiny 数据表中添加自定义按钮?
【发布时间】:2018-12-03 19:54:39
【问题描述】:

有一个选项可以在 datatables.net 网站上添加自定义按钮。如何在 R Shiny 应用程序中对其进行编码?一个按钮和观察者的基本 R 代码示例将很高兴看到。

这是来自https://datatables.net/extensions/buttons/examples/initialisation/custom.html的JS代码

$(document).ready(function() {
    $('#example').DataTable( {
        dom: 'Bfrtip',
        buttons: [
            {
                text: 'My button',
                action: function ( e, dt, node, config ) {
                    alert( 'Button activated' );
                }
            }
        ]
    } );
} );

谢谢!

【问题讨论】:

    标签: r user-interface button shiny dt


    【解决方案1】:

    您不需要使用 Javascript,除了操作。你可以这样做:

    library(DT)
    datatable(iris,
              extensions = 'Buttons',
              options = list(
                dom = 'Bfrtip',
                buttons = list(
                  "copy",
                  list(
                    extend = "collection",
                    text = 'test',
                    action = DT::JS("function ( e, dt, node, config ) {
                                        alert( 'Button activated' );
                                    }")
                  )
                )
              )
    )
    

    要将某些内容从 Javascript 传递到闪亮的服务器,请使用 Shiny.setInputValue

    library(shiny)
    library(DT)
    
    ui <- basicPage(
      DTOutput("dtable")
    )
    
    server <- function(input, output, session){
      output$dtable <- renderDT(
        datatable(iris,
                  extensions = 'Buttons',
                  options = list(
                    dom = 'Bfrtip',
                    buttons = list(
                      "copy",
                      list(
                        extend = "collection",
                        text = 'test',
                        action = DT::JS("function ( e, dt, node, config ) {
                                          Shiny.setInputValue('test', true, {priority: 'event'});
                                       }")
                      )
                    )
                  )
        )
      )
    
      observeEvent(input$test, {
          print("hello")
      })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 非常感谢您的提示! “测试”按钮出现在正确的位置,没有“操作”。但是当我添加 action = DT::JS("function ( e, dt, node, config ) {Shiny.setInputValue('test', true, priority: 'event');}") - 窗口中什么也没有显示。诀窍在哪里?
    • @andrii “窗口”是什么意思?您想显示警报吗? Shiny.setInputValue 是将变量传递给闪亮的服务器。如果您只想要一个警报,请将其删除并替换为 alert(...)
    • 我的意思是仪表板没有出现嵌入按钮“测试”-空白屏幕-当我添加“操作”时
    • 我使用这个代码示例用于添加“下载所有数据”按钮github.com/rstudio/DT/issues/267thx
    • 不错!我将此用于我在此问题中找到的提交/删除选择,Stephane 也回答了:stackoverflow.com/questions/55550126/…
    【解决方案2】:

    我很欣赏 Stephane Laurent 的回答并找到了丢失的东西。

    周围必须有一对括号 {}
    priority: 'event'
    

    喜欢,

    Shiny.setInputValue('test', true, {priority: 'event'});
    

    https://shiny.rstudio.com/articles/communicating-with-js.html

    【讨论】:

      猜你喜欢
      • 2021-02-10
      • 2021-01-31
      • 1970-01-01
      • 2014-05-29
      • 2020-11-29
      • 2016-02-02
      • 2021-11-11
      • 1970-01-01
      • 2018-03-13
      相关资源
      最近更新 更多