简体   繁体   English

闪亮绑定输入 class 丢失在 R Shiny 与 DT 和 Z19B13BE0F34E065AB672ZB203

[英]shiny-bound-input class lost in R Shiny with DT and data.table

I am trying to build a shiny app that uses dynamically created inputs within a data.table with the help of the DT package.我正在尝试构建一个 shiny 应用程序,该应用程序在DT data.table的帮助下使用 data.table 中动态创建的输入。 In the reproducible example below, the shiny-input input$Sel_Group_1 created within the DT-output depends on the value chosen in the shiny-input input$selectGroup (values a or b).在下面的可重现示例中,在 DT 输出中创建的闪亮输入input$Sel_Group_1取决于在闪亮输入input$selectGroup中选择的值(值 a 或 b)。 The selected item (values c,d,f,g) are then shown in the textoutput output$selectedItem .所选项目(值 c,d,f,g)然后显示在文本output$selectedItem中。

When I initially launch the app everything works fine, but as soon as I update the value in input$selectGroup the binding of the element input$Sel_Group_1 is lost and the textoutput output$selectedItem does not react any more.当我最初启动应用程序时,一切正常,但是一旦我更新input$selectGroup中的值,元素input$Sel_Group_1 Sel_Group_1 的绑定就会丢失,并且文本output$selectedItem不再反应。 Upon inspecting the element input$Sel_Group_1 I found out that the class attribute "shiny-bound-input" is lost as soon as the value in input$selectGroup is updated.在检查元素input$Sel_Group_1后,我发现一旦input$selectGroup中的值更新,class 属性“shiny-bound-input”就会丢失。

Many thanks in advance for help on this topic!非常感谢您在此主题上的帮助!

library(shiny)
library(DT)
library(data.table)


data <- data.table(Group = c("a", "a", "b", "b"), Item = c("c", "d", "f", "g"))

ui <- fluidPage(
  selectInput("selectGroup", "Select Group", c("a", "b")),
  DT::dataTableOutput('ItemSelection'),
  textOutput("selectedItem")
)

server <- function(input, output, session) {
  output$ItemSelection <- DT::renderDT(
    {
      data_selected <- data[Group == input$selectGroup]
      data_unique <- unique(data[Group == input$selectGroup, .(Group)])

      data_unique$Item_Selection[1] <-
        as.character(selectInput(
          "Sel_Group_1",
          label = NULL,
          choices = unique(data_selected[Group %in% data_unique[1], Item]),
          width = "100px"
        ))

      return(data_unique)

    }
    , escape = FALSE, selection = 'none', server = FALSE,
    options = list(dom = 't', paging = FALSE, ordering = FALSE),
    rownames = FALSE,
    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());")
  )


  output$selectedItem <- renderText(paste0("Item selected is: ", input$Sel_Group_1))
}

shinyApp(ui, server)

You have to unbind before the table is rerendered:您必须在重新呈现表之前取消绑定:

library(shiny)
library(DT)
library(data.table)


data <- data.table(Group = c("a", "a", "b", "b"), Item = c("c", "d", "f", "g"))

ui <- fluidPage(
  tags$head(tags$script(
    HTML(
      "Shiny.addCustomMessageHandler('unbindDT', function(id) {
        var $table = $('#'+id).find('table');
        if($table.length > 0){
          Shiny.unbindAll($table.DataTable().table().node());
        }
      })")
  )),
  selectInput("selectGroup", "Select Group", c("a", "b")),
  DT::dataTableOutput('ItemSelection'),
  textOutput("selectedItem")
)

server <- function(input, output, session) {
  output$ItemSelection <- DT::renderDT(
    {
      data_selected <- data[Group == input$selectGroup]
      data_unique <- unique(data[Group == input$selectGroup, .(Group)])
      
      data_unique$Item_Selection[1] <-
        as.character(selectInput(
          "Sel_Group_1",
          label = NULL,
          choices = unique(data_selected[Group %in% data_unique[1], Item]),
          width = "100px"
        ))
      
      datatable(
        data_unique, escape = FALSE, selection = 'none',
        options = list(
          dom = 't', 
          paging = FALSE, 
          ordering = FALSE,
          preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
          drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); }')
        ),
        rownames = FALSE
      )
      
    }, server = FALSE)
  
  observeEvent(input$selectGroup, {
    session$sendCustomMessage("unbindDT", "ItemSelection")
  })
  
  
  output$selectedItem <- renderText(paste0("Item selected is: ", input$Sel_Group_1))
}

shinyApp(ui, server)

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

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