简体   繁体   English

R Shiny Datatable 中的嵌入式输入 - javascript 问题

[英]Embedded inputs in R Shiny Datatable - javascript issue

I have an R/Shiny app containing a datatable from the DT package.我有一个包含来自 DT 包的数据表的 R/Shiny 应用程序。

Following this thread render dropdown for single column in DT shiny , I've been able to embed action buttons into a column in my datatable, which trigger a set of corresponding observers.按照这个线程为 DT 中的单列渲染下拉列表,我已经能够将操作按钮嵌入到我的数据表中的一列中,这会触发一组相应的观察者。

However, when the datatable is paginated, my action buttons will only function correctly for those buttons on the first page.但是,当数据表被分页时,我的操作按钮只会对第一页上的那些按钮正常工作。 Buttons on subsequent pages don't work.后续页面上的按钮不起作用。 This remains the case even if I reorder the data using column sorting, as any buttons which were on page 2+ as of the initial render will not work even if they are reordered onto page 1.即使我使用列排序对数据重新排序,情况仍然如此,因为在初始渲染时第 2+ 页上的任何按钮即使重新排序到第 1 页也将不起作用。

I expect the problem is in how the callback argument is using javascript (which is unfortunately over my head) to render the action buttons correctly.我希望问题在于回调参数如何使用 javascript(不幸的是我无法理解)来正确呈现操作按钮。 Can anyone advise how to get the action buttons working on subsequent pages?谁能建议如何让操作按钮在后续页面上工作?

Here is my minimal reprex, using mtcars data:这是我的最小 reprex,使用 mtcars 数据:

library(shiny)
library(DT)

ui <- fluidPage(
    titlePanel("reprex1")
    ,fluidRow(
        dataTableOutput("dt1")
    )
)

server <- function(input, output) {
    output$dt1 <- renderDataTable({
        mtlocal <- mtcars
        for(n in 1:nrow(mtlocal)){
            mtlocal$actionbutton[[n]] <- as.character(
                actionButton(
                    paste0("buttonpress",n), label = paste0("buttonpress",n)
                )
            )
        }
        datatable(
            mtlocal
            ,escape = FALSE
            ,selection = "none"
            ,callback = JS("table.rows().every(function(i, tab, row) {
        var $this = $(this.node());
        $this.attr('id', this.data()[0]);
        $this.addClass('shiny-input-container');
      });
      Shiny.unbindAll(table.table().node());
      Shiny.bindAll(table.table().node());")
        )
    }, server = FALSE)

    lapply(
        1:nrow(mtcars),function(x){
            observeEvent(
                input[[paste0("buttonpress",x)]],{
                    showModal(
                        modalDialog(
                            h2(paste0("You clicked on button ",x,"!"))
                        )
                    )
                }
            )       
        }
    )
}

# Run the application 
shinyApp(ui = ui, server = server)

The callback is executed only once, and then Shiny.bind/unbind is lost when the table is redrawn. callback只执行一次,然后Shiny.bind/unbind在表重绘时丢失。 You have to use the options preDrawCallback and drawCallback :您必须使用选项preDrawCallbackdrawCallback

datatable(
  mtlocal
  , escape = FALSE
  , selection = "none"
  , options = list(
    preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
    drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')
  )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM