【问题标题】:Is there any way to add custom R-function action to a button in DT?有什么方法可以将自定义 R 功能操作添加到 DT 中的按钮?
【发布时间】:2019-04-20 10:59:56
【问题描述】:

我正在尝试将执行 R 函数定义的操作的自定义按钮添加到我的数据表中。我在 R 代码中使用了与 Datatables manual 中的 Javascript 代码相同的选项列表,但它不起作用。

这是来自Datatables manual的代码:

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

这是我在 R 中的代码:

require(DT)
DT::datatable(iris,
    extensions = 'Buttons',
    options = list(
        dom = 'Bfrtip',
        buttons = list(
            list(
                text = 'test',
                action = print('1')
                )
        )
    )
)

执行它我收到一个错误:

Error in if (extend != "collection") extend else listButtons(cfg) : 
  argument is of length zero

【问题讨论】:

    标签: r datatables dt


    【解决方案1】:

    你必须设置extend = "collection",像这样:

    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,而不是 R 命令。但是,如果将数据表放在闪亮的应用程序中,则可以通过单击自定义按钮来执行 R 命令。类似的东西:

    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);
                                       }")
                      )
                    )
                  )
        )
      )
    
      observeEvent(input$test, {
        if(input$test){
          print("hello")
        }
      })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 这行得通,但我稍微更改了您的代码,以便将按钮用作触发器。所以我添加了priority: "event" 选项,正如this Shiny manual page 的“值与事件”部分所述。
    • 你能解释一下你为什么使用if(input$test)吗?没有它,代码对我来说很好。
    • @asvx 你说得对,if 没用。你使用priority: "event" 是对的。不知道priority: "event",之前用过一个技巧,在值不变的时候触发输入变化,但是用priority: "event"肯定更好。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多