【问题标题】:adding regular button to datatable - shiny app向数据表添加常规按钮 - 闪亮的应用程序
【发布时间】:2021-11-19 19:50:53
【问题描述】:

我在闪亮的应用程序中使用数据表。 我想在搜索按钮附近添加一个按钮。 当我点击按钮时,我想调用函数:observeEvent(input[["btn"]] 这是我的代码:

   DT::renderDataTable(rownames = FALSE,                         
                               DT::datatable(my_df,extensions = 'Buttons',  
                  options = list(info  = FALSE, paging = FALSE, 
                                 dom='Bfrtip', buttons= list('copy')
                                  )))

它看起来很棒,但我想要一个普通按钮来调用而不是复制按钮 这个函数:observeEvent(input[["btn"]] 知道我该怎么做吗?

【问题讨论】:

  • 你可能需要一些条件面板

标签: r shiny datatable


【解决方案1】:

让我们定义一个自定义按钮。

将按钮标签更改为text,更改setInputValue 中的闪亮输入ID,将mydf_btn 更改为您想要的任何内容。

library(shiny)

ui <- fluidPage(
  DT::DTOutput("mydf")
)

server <- function(input, output, session) {
    output$mydf <- DT::renderDataTable(                       
                        DT::datatable(
                            iris,extensions = 'Buttons', rownames = FALSE,  
                            options = list(
                            info  = FALSE, paging = FALSE, dom='lfBrtip', 
                            buttons= list(
                                list(
                                    extend = 'collection',
                                    text = 'My Action',
                                    action = DT::JS(
                                    "function() {
                                        var node = this[0].node;
                                        var value = $(node).attr('data-value') || 0;
                                        value ++;
                                        $(node).attr('data-value', value);
                                        Shiny.setInputValue('mydf_btn', value, {priority: 'event'});
                                    }"
                                    )
                                )
                            )
                          )
                        )
    )
    observe(print(input$mydf_btn))
}

shinyApp(ui, server)

【讨论】:

  • TY!其作品!我怎样才能通过css改变?
  • 你的意思是仍然通过 CSS 改变?
  • 例如,如果我想更改按钮的背景颜色,或者将按钮放在右侧,靠近搜索按钮
猜你喜欢
  • 2017-05-11
  • 2020-05-09
  • 2018-07-06
  • 2020-08-22
  • 1970-01-01
  • 2014-10-04
  • 2020-06-16
  • 2017-07-27
  • 2022-01-20
相关资源
最近更新 更多