如何在R Shiny数据表中添加自定义按钮? [英] How to add custom button in R Shiny datatable?

查看:338
本文介绍了如何在R Shiny数据表中添加自定义按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个选项可以在datatables.net网站上添加自定义按钮。如何在R Shiny应用中进行编码?一个按钮和观察者的基本R代码示例将非常有趣。

There is an option to add a custom button on datatables.net site. How it can be coded in R Shiny app? A basic R code example for one button and observer will be great to see.

这里是 https://datatables.net/extensions/buttons/examples/initialisation/custom.html

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

谢谢!

推荐答案

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

You don't need to use Javascript, except for the action. You can do:

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

To pass something from Javascript to the shiny server, use 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)

这篇关于如何在R Shiny数据表中添加自定义按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆